waybill.model.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. <?php
  2. /**
  3. * 运单模板模型
  4. *
  5. *
  6. *
  7. *
  8. */
  9. defined('InShopNC') or exit('Access Invalid!');
  10. class waybillModel extends Model{
  11. const WAYBILL_PIXEL_CONSTANT = 3.8;
  12. const WAYBILL_USABLE = 1;
  13. public function __construct(){
  14. parent::__construct('waybill');
  15. }
  16. /**
  17. * 读取列表
  18. * @param array $condition
  19. *
  20. */
  21. public function getWaybillList($condition, $page='', $order='waybill_usable desc', $field='*') {
  22. $waybill_list = $this->field($field)->where($condition)->page($page)->order($order)->select();
  23. foreach ($waybill_list as $key => $value) {
  24. $waybill_list[$key]['waybill_image_url'] = getWaybillImageUrl($value['waybill_image']);
  25. $waybill_list[$key]['waybill_usable_text'] = $value['waybill_usable'] ? '是' : '否';
  26. $waybill_list[$key]['waybill_type_text'] = $value['store_id'] ? '用户模板' : '平台模板';
  27. }
  28. return $waybill_list;
  29. }
  30. /**
  31. * 读取可用列表
  32. * @param int $express_id 快递公司编号
  33. * @param int $store_id 店铺编号
  34. *
  35. */
  36. public function getWaybillUsableList($express_id, $store_id = 0) {
  37. $condition['express_id'] = $express_id;
  38. $condition['waybill_usable'] = self::WAYBILL_USABLE;
  39. if($store_id > 0) {
  40. $condition['store_id'] = array('in', "0,{$store_id}");
  41. } else {
  42. $condition['store_id'] = 0;
  43. }
  44. return $this->getWaybillList($condition, '', 'waybill_usable desc', '*');
  45. }
  46. /**
  47. * 读取平台模板列表
  48. * @param int $page 分页数
  49. */
  50. public function getWaybillAdminList($page = '') {
  51. $condition = array();
  52. $condition['store_id'] = 0;
  53. return $this->getWaybillList($condition, $page);
  54. }
  55. /**
  56. * 读取商家模板列表
  57. * @param int $store_id 店铺编号
  58. */
  59. public function getWaybillSellerList($store_id = 0) {
  60. if($store_id <= 0) {
  61. return null;
  62. }
  63. $condition = array();
  64. $condition['store_id'] = $store_id;
  65. return $this->getWaybillList($condition);
  66. }
  67. /**
  68. * 读取单条记录
  69. * @param array $condition
  70. *
  71. */
  72. public function getWaybillInfo($condition) {
  73. $waybill_info = $this->where($condition)->find();
  74. if($waybill_info) {
  75. $waybill_info['waybill_image_url'] = getWaybillImageUrl($waybill_info['waybill_image']);
  76. $waybill_info['waybill_pixel_width'] = $waybill_info['waybill_width'] * self::WAYBILL_PIXEL_CONSTANT;
  77. $waybill_info['waybill_pixel_height'] = $waybill_info['waybill_height'] * self::WAYBILL_PIXEL_CONSTANT;
  78. $waybill_info['waybill_pixel_top'] = $waybill_info['waybill_top'] * self::WAYBILL_PIXEL_CONSTANT;
  79. $waybill_info['waybill_pixel_left'] = $waybill_info['waybill_left'] * self::WAYBILL_PIXEL_CONSTANT;
  80. if(!empty($waybill_info['waybill_data'])) {
  81. $waybill_info['waybill_data'] = unserialize($waybill_info['waybill_data']);
  82. //整理打印模板
  83. $waybill_item = $this->getWaybillItemList();
  84. foreach ($waybill_info['waybill_data'] as $key => $value) {
  85. $waybill_info['waybill_data'][$key]['content'] = $waybill_item[$key]['item_text'];
  86. }
  87. }
  88. }
  89. return $waybill_info;
  90. }
  91. /**
  92. * 根据编号读取单条记录
  93. * @param array $condition
  94. *
  95. */
  96. public function getWaybillInfoByID($waybill_id) {
  97. $waybill_id = intval($waybill_id);
  98. if($waybill_id <= 0) {
  99. return false;
  100. }
  101. $waybill_info = $this->getWaybillInfo(array('waybill_id' => $waybill_id));
  102. return $waybill_info;
  103. }
  104. /**
  105. * 获取设计数据
  106. * @param int $waybill_id
  107. * @param int $store_id 提供店铺编号时验证模板的所属店铺
  108. */
  109. public function getWaybillDesignInfo($waybill_id, $store_id = 0) {
  110. $waybill_id = intval($waybill_id);
  111. if($waybill_id <= 0) {
  112. return array('error' => '运单模板不存在');
  113. }
  114. $waybill_info = $this->getWaybillInfoByID($waybill_id);
  115. if(!$waybill_info) {
  116. return array('error' => '运单模板不存在');
  117. }
  118. if($store_id > 0 && $waybill_info['store_id'] != $store_id) {
  119. return array('error' => '运单模板不存在');
  120. }
  121. $waybill_info_data = $waybill_info['waybill_data'];
  122. unset($waybill_info['waybill_data']);
  123. //项目列表
  124. $waybill_item_list = $this->getWaybillItemList();
  125. if(!empty($waybill_info_data)) {
  126. foreach ($waybill_info_data as $key => $value) {
  127. $waybill_info_data[$key]['item_text'] = $waybill_item_list[$key]['item_text'];
  128. }
  129. }
  130. foreach ($waybill_item_list as $key => $value) {
  131. $waybill_item_list[$key]['check'] = $waybill_info_data[$key]['check'] ? 'checked' : '';
  132. $waybill_item_list[$key]['width'] = $waybill_info_data[$key]['width'] ? $waybill_info_data[$key]['width'] : '0';
  133. $waybill_item_list[$key]['height'] = $waybill_info_data[$key]['height'] ? $waybill_info_data[$key]['height'] : '0';
  134. $waybill_item_list[$key]['top'] = $waybill_info_data[$key]['top'] ? $waybill_info_data[$key]['top'] : '0';
  135. $waybill_item_list[$key]['left'] = $waybill_info_data[$key]['left'] ? $waybill_info_data[$key]['left'] : '0';
  136. }
  137. return array(
  138. 'waybill_info' => $waybill_info,
  139. 'waybill_info_data' => $waybill_info_data,
  140. 'waybill_item_list' => $waybill_item_list,
  141. );
  142. }
  143. /*
  144. * 增加
  145. * @param array $param
  146. * @return bool
  147. */
  148. public function addWaybill($param){
  149. return $this->insert($param);
  150. }
  151. /*
  152. * 更新
  153. * @param array $update
  154. * @param array $condition
  155. * @return bool
  156. */
  157. public function editWaybill($update, $condition) {
  158. return $this->where($condition)->update($update);
  159. }
  160. /*
  161. * 更新
  162. * @param array $waybill_data
  163. * @param int $waybill_id
  164. * @param int $store_id
  165. * @return bool
  166. */
  167. public function editWaybillDataByID($waybill_data, $waybill_id, $store_id = 0) {
  168. $waybill_id = intval($waybill_id);
  169. if($waybill_id <= 0) {
  170. return false;
  171. }
  172. $update = array();
  173. $update['waybill_data'] = serialize($waybill_data);
  174. $condition = array();
  175. $condition['waybill_id'] = $waybill_id;
  176. if(!empty($store_id)) {
  177. $condition['store_id'] = $store_id;
  178. }
  179. return $this->editWaybill($update, $condition);
  180. }
  181. /**
  182. * 保存
  183. */
  184. public function saveWaybill($post, $store_id = 0) {
  185. $param = array();
  186. $param['waybill_name'] = $post['waybill_name'];
  187. $param['waybill_width'] = $post['waybill_width'];
  188. $param['waybill_height'] = $post['waybill_height'];
  189. $param['waybill_left'] = $post['waybill_left'];
  190. $param['waybill_top'] = $post['waybill_top'];
  191. $param['waybill_usable'] = $post['waybill_usable'];
  192. $param['store_id'] = $store_id;
  193. list($param['express_id'], $param['express_name']) = explode('|', $_POST['waybill_express']);
  194. //图片上传
  195. $waybill_image = $this->_waybill_image_upload();
  196. if(!isset($waybill_image['error'])) {
  197. $param['waybill_image'] = $waybill_image;
  198. if(!empty($param['old_waybill_image'])) {
  199. $this->delWaybillImage($_POST['old_waybill_image']);
  200. }
  201. }
  202. //验证数据
  203. $error = $this->validWaybill($param);
  204. if ($error != ''){
  205. return array('error' => $error);
  206. }
  207. if(empty($post['waybill_id'])) {
  208. //添加
  209. $result = $this->addWaybill($param);
  210. $waybill_id = $result;
  211. } else {
  212. //编辑
  213. $condition = array();
  214. $condition['waybill_id'] = intval($post['waybill_id']);
  215. if($store_id > 0) {
  216. $condition['store_id'] = $store_id;
  217. }
  218. $result = $this->editWaybill($param, $condition);
  219. $waybill_id = $post['waybill_id'];
  220. }
  221. if($result) {
  222. return $waybill_id;
  223. } else {
  224. return array('error' => '保存失败');
  225. }
  226. }
  227. /**
  228. * 图片上传
  229. */
  230. private function _waybill_image_upload() {
  231. $upload = new UploadFile();
  232. $upload->set('default_dir', ATTACH_WAYBILL);
  233. $upload->set('allow_type', array('jpg','jpeg','png'));
  234. $result = $upload->upfile('waybill_image');
  235. if($result) {
  236. return $upload->file_name;
  237. } else {
  238. return array('error' => $upload->error);
  239. }
  240. }
  241. /*
  242. * 删除
  243. * @param array $condition
  244. * @return bool
  245. */
  246. public function delWaybill($condition) {
  247. $waybill_id_string = '';
  248. //删除模板图片
  249. $wayblii_list = $this->getWaybillList($condition, null);
  250. foreach ($wayblii_list as $value) {
  251. $this->delWaybillImage($value['waybill_image']);
  252. $waybill_id_string .= $value['waybill_id'];
  253. }
  254. //删除已经建立的绑定
  255. $model_store_waybill = Model('store_waybill');
  256. $model_store_waybill->delStoreWaybill(array('waybill_id' => array('in', $waybill_id_string)));
  257. return $this->where($condition)->delete();
  258. }
  259. public function delWaybillImage($image_name) {
  260. $image = BASE_UPLOAD_PATH . DS . ATTACH_WAYBILL . DS . $image_name;
  261. if(is_file($image)) {
  262. @unlink($image);
  263. }
  264. }
  265. /**
  266. * 获取运单项目列表
  267. */
  268. public function getWaybillItemList() {
  269. $item = array(
  270. 'buyer_name' => array('item_text' => '收货人'),
  271. 'buyer_area' => array('item_text' => '收货人地区'),
  272. 'buyer_address' => array('item_text' => '收货人地址'),
  273. 'buyer_mobile' => array('item_text' => '收货人手机'),
  274. 'buyer_phone' => array('item_text' => '收货人电话'),
  275. 'seller_name' => array('item_text' => '发货人'),
  276. 'seller_area' => array('item_text' => '发货人地区'),
  277. 'seller_address' => array('item_text' => '发货人地址'),
  278. 'seller_phone' => array('item_text' => '发货人电话'),
  279. 'seller_company' => array('item_text' => '发货人公司'),
  280. );
  281. return $item;
  282. }
  283. /*
  284. * 验证
  285. * @param array $input
  286. * @return $error
  287. */
  288. public function validWaybill($input) {
  289. $obj_validate = new Validator();
  290. $obj_validate->validateparam = array(
  291. array("input"=>$input['waybill_name'], "require"=>"true", "message"=>'模板名称不能为空'),
  292. array("input"=>$input['waybill_width'], "require"=>"true", "message"=>'宽度不能为空'),
  293. array("input"=>$input['waybill_height'], "require"=>"true", "message"=>'高度不能为空'),
  294. );
  295. return $obj_validate->validate();
  296. }
  297. /**
  298. * 根据订单信息获取打印数据
  299. * @param array $order_info 需要包括扩展数据
  300. */
  301. public function getPrintInfoByOrderInfo($order_info) {
  302. $model_daddress = Model('daddress');
  303. //获取打印数据
  304. $print_info = array();
  305. $daddress_id = $order_info['extend_order_common']['daddress_id'];
  306. $daddress_info = array();
  307. if(!empty($daddress_id)) {
  308. $daddress_info = $model_daddress->getAddressInfo(array('address_id' => $daddress_id));
  309. }
  310. $reciver_info = $order_info['extend_order_common']['reciver_info'];
  311. $print_info['buyer_name'] = $order_info['extend_order_common']['reciver_name'];
  312. $print_info['buyer_area'] = $reciver_info['area'];
  313. $print_info['buyer_address'] = $reciver_info['street'];
  314. $print_info['buyer_mobile'] = $reciver_info['mob_phone'];
  315. $print_info['buyer_phone'] = $reciver_info['tel_phone'];
  316. $print_info['seller_name'] = $daddress_info['seller_name'];
  317. $print_info['seller_area'] = $daddress_info['area_info'];
  318. $print_info['seller_address'] = $daddress_info['address'];
  319. $print_info['seller_phone'] = $daddress_info['telphone'];
  320. $print_info['seller_company'] = $daddress_info['company'];
  321. return $print_info;
  322. }
  323. }