Order_operation.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. require_once APPPATH.'/models/Model_fullorder.php';
  3. require_once APPPATH.'/models/Model_fullordersmt.php';
  4. require_once APPPATH.'/models/Model_warehouse.php';
  5. require_once APPPATH.'/models/Model_headorder.php';
  6. require_once APPPATH.'/models/Model_typeclass.php';
  7. class Order_operation{
  8. var $ci;
  9. public function __construct()
  10. {
  11. $this->ci = &get_instance();
  12. $this->fullorder=new Model_fullorder();
  13. $this->fullordersmt=new Model_fullordersmt();
  14. $this->warehouse=new Model_warehouse();
  15. $this->headorder=new Model_headorder();
  16. $this->typeclass=new Model_typeclass();
  17. $this->ci->load->library('order_process');
  18. }
  19. /***
  20. * action str
  21. * order model order
  22. */
  23. public function handle($action,$ordernumber){
  24. switch($action){
  25. case "add":
  26. return $this->add($ordernumber);
  27. break;
  28. case "update":
  29. $this->update($ordernumber);
  30. break;
  31. case "delete":
  32. $this->delete($ordernumber);
  33. break;
  34. }
  35. }
  36. public function add($orderinfo){
  37. $where='1=1';
  38. try {
  39. $thisdata=$this->fullorder->find('orderinfo='.$orderinfo);
  40. if (!$thisdata) {
  41. $thisdata=$this->fullordersmt->find('orderinfo='.$orderinfo);
  42. }
  43. if (!$thisdata) {
  44. return false;
  45. }
  46. $this->ci->db->trans_begin();
  47. $headOrder_id=$this->headorder->setOrder($thisdata);//model insert
  48. if (!$headOrder_id) {
  49. throw new Exception('something wrong');
  50. }
  51. $whlable=explode('|', trim($thisdata['whlabel'], "|"));
  52. $whlable=array_map(function ($v) {
  53. return substr($v, 0, (strpos($v, '-')));
  54. }, $whlable);
  55. if (stripos($thisdata['fpdata'], ';') !== false) {
  56. $fpdata = explode(';', rtrim($thisdata['fpdata'], ';'));
  57. foreach ($fpdata as $k=>$v) {
  58. $fpdata[$k] = explode('|', $v);
  59. }
  60. }
  61. $typeClass=$this->typeclass->find_all();
  62. $n=1;
  63. foreach ($fpdata as $k=>$fp) {
  64. if (stripos($fp[0], '128')>-1||stripos($fp[1], 'Wigs')>-1) {//头套
  65. $sku=$this->sortsku($typeClass, $fp);
  66. $skustr="";
  67. foreach ($sku as $v) {
  68. if (isset($v['zh'])&&!empty($v['zh'])) {
  69. $skustr.=$v['zh'].' ';
  70. }
  71. }
  72. $i=$fp[2];
  73. for ($i=$fp[2];$i>0;$i--) {
  74. $orders[]=[
  75. 'fpdata'=>$fp[0],
  76. 'warehouse'=>$thisdata['warehouse'],
  77. 'whlabel'=>$whlable[$k],
  78. 'number'=>$thisdata['number'].'-'.$n,
  79. 'orderinfo'=>$thisdata['orderinfo'].'-'.$n,
  80. 'p_order'=>$thisdata['orderinfo'],
  81. 'p_number'=>$thisdata['number'],
  82. 'orderremarks'=>$thisdata['orderremarks'],//订单品名
  83. 'product'=>$thisdata['product'],
  84. 'sku'=>json_encode($sku),
  85. 'shipremarks'=>$skustr,
  86. ];
  87. $n++;
  88. }
  89. }
  90. }
  91. if (!empty($orders)) {
  92. foreach ($orders as $order) {
  93. $process=$this->ci->order_process->createOrderProcess($order);
  94. }
  95. }
  96. if ($this->ci->db->trans_status() == false) {
  97. throw new Exception('something wrong!');
  98. }
  99. $this->ci->db->trans_commit();
  100. return true;
  101. }catch(Exception $e){
  102. $this->ci->db->trans_rollback();
  103. return false;
  104. }
  105. }
  106. public function update($orderinfo){
  107. $flag=false;
  108. $where='1=1';
  109. // $orderinfo=22007131647400;
  110. $thisdata=$this->fullorder->find('orderinfo='.$orderinfo);
  111. if(!$thisdata){
  112. $thisdata=$this->fullordersmt->find('orderinfo='.$orderinfo);
  113. }
  114. if(!$thisdata){
  115. return false;
  116. }
  117. $query=$this->ci->db->select('a.whlabel,a.p_order,count(a.whlabel) as num')
  118. ->from('headorder_item as a')
  119. ->join('process as b',' a.`id`=b.`order_id`','inner')
  120. ->join('flow_status as c','b.`status_id`=c.id')
  121. ->where('a.p_order='.$orderinfo)
  122. ->where('b.state="'.Model_Process::STATUS_PROCESSING.'"')
  123. ->group_by('a.whlabel')
  124. ->order_by('a.whlabel ASC','c.order ASC')
  125. ->get();
  126. $headOrder=$query->result_array();
  127. $headOrder=array_combine(array_column($headOrder,'whlabel'),$headOrder);
  128. $whlable=explode('|',trim($thisdata['whlabel'],"|"));
  129. $whlable=array_map(function($v){
  130. return substr($v,0,(strpos($v,'-')));
  131. },$whlable);
  132. if(stripos($thisdata['fpdata'],';') !== false)
  133. {
  134. $fpdata = explode(';',rtrim($thisdata['fpdata'],';'));
  135. foreach ($fpdata as $k=>$v)
  136. {
  137. $fpdata[$k] = explode('|',$v);
  138. }
  139. }
  140. $n=$this->ci->db
  141. ->from('headorder_item')
  142. ->where('p_order='.$orderinfo)
  143. ->count_all_results();
  144. $typeClass=$this->typeclass->find_all();
  145. foreach ($fpdata as $k=>$fp) {
  146. if(stripos($fp[0],'128')>-1||stripos($fp[1],'Wigs')>-1){//头套
  147. $label=$whlable[$k]?$whlable[$k]:'';
  148. if(!isset($headOrder[$label])||empty($headOrder[$label])){//新增
  149. $add_num=$fp[2];
  150. $this->additem($fp,$typeClass,$thisdata,$add_num,$label,$n);
  151. $n+=$add_num;
  152. }else{
  153. $c_num=$fp[2];
  154. $l_num=$headOrder[$label]['num'];
  155. //比较数量 多 增
  156. if($c_num>$l_num){
  157. $add_num=$c_num-$l_num;
  158. $this->additem($fp,$typeClass,$thisdata,$add_num,$label,$n);
  159. $n+=$add_num;
  160. }elseif($c_num<$l_num){
  161. $del_num=$l_num-$c_num;
  162. $this->delitem($label,$del_num);
  163. }else{//不做任何操作
  164. continue;
  165. }
  166. }
  167. }
  168. }
  169. return true;
  170. }
  171. private function additem($fp,$typeClass,$thisdata,$add_num,$label,$n){
  172. $sku=$this->sortsku($typeClass,$fp);
  173. $skustr="";
  174. foreach($sku as $v){
  175. if(isset($v['zh'])&&!empty($v['zh'])){
  176. $skustr.=$v['zh'].' ';
  177. }
  178. }
  179. for($i=$n;$i<$n+$add_num+1;$i++){
  180. $n+=1;
  181. $orders[]=[
  182. 'fpdata'=>$fp[0],
  183. 'warehouse'=>$thisdata['warehouse'],
  184. 'whlabel'=>$label,
  185. 'number'=>$thisdata['number'].'-'.$n,
  186. 'orderinfo'=>$thisdata['orderinfo'].'-'.$n,
  187. 'p_order'=>$thisdata['orderinfo'],
  188. 'p_number'=>$thisdata['number'],
  189. 'orderremarks'=>$thisdata['orderremarks'],//订单品名
  190. 'product'=>$thisdata['product'],
  191. 'sku'=>json_encode($sku),
  192. 'shipremarks'=>$skustr,
  193. ];
  194. }
  195. //addprocess 完成
  196. foreach($orders as $order){
  197. $this->ci->order_process->createOrderProcess($order);
  198. }
  199. return true;
  200. }
  201. private function delitem($label,$num){
  202. $query=$this->ci->db->select('a.id,a.whlabel,a.p_order,b.id as process_id,c.code,c.name,c.order')
  203. ->from('headorder_item as a')
  204. ->join('process as b',' a.`id`=b.`order_id`','inner')
  205. ->join('flow_status as c','b.`status_id`=c.id')
  206. ->where('a.whlabel='.$label)
  207. ->where('b.state="'.Model_Process::STATUS_PROCESSING.'"')
  208. ->order_by('c.order ASC')
  209. ->limit($num)
  210. ->get();
  211. $del_items=$query->result_array();
  212. return $this->db->where_in('id',array_column($del_items,'process_id'))->update('process',[
  213. 'state'=>Model_Process::STATUS_CANCELED,
  214. ]);
  215. }
  216. private function sortsku($typeClass,$fp){
  217. $arr = explode(',',$fp[0]);
  218. $length=$arr[0];
  219. $arr1=explode('-',$arr[1]);
  220. array_push($arr1,$length);
  221. $ids=array_column($typeClass,'id');
  222. $typeClass=array_combine($ids,$typeClass);
  223. foreach($arr1 as $v){
  224. if(!empty($v)&&isset($typeClass[$v])&&!empty($typeClass[$v])){
  225. $sku[$v]=$typeClass[$v];
  226. }
  227. }
  228. // $cptt = array(16=>128,18=>'',6=>30,10=>72,9=>'');//头套
  229. $pm = array(13=>'',22=>'',8=>'',15=>'',18=>'','100'=>'','dc'=>'','c'=>'',14=>'',12=>'',25=>'',26=>'',27=>'',10=>'',6=>'',9=>'',999=>'',9999=>'');//品名顺序
  230. $res=[];
  231. $sku=array_combine(array_column($sku,'classid'),$sku);
  232. foreach ($pm as $k => $v) {
  233. if(isset($sku[$k])&&!empty($sku[$k])){
  234. $res[]=$sku[$k];
  235. }
  236. }
  237. return $res;
  238. }
  239. public function delete($orderinfo){
  240. $query=$this->ci->db->select('a.id,a.whlabel,a.p_order,b.id as process_id,c.code,c.name,c.order')
  241. ->from('headorder_item as a')
  242. ->join('process as b',' a.`id`=b.`order_id`','inner')
  243. ->join('flow_status as c','b.`status_id`=c.id')
  244. ->where('a.p_order='.$orderinfo)
  245. ->where('b.state="'.Model_Process::STATUS_PROCESSING.'"')
  246. ->order_by('c.order ASC')
  247. ->get();
  248. $del_items=$query->result_array();
  249. return $this->db->where_in('id',array_column($del_items,'process_id'))->update('process',[
  250. 'state'=>Model_Process::STATUS_CANCELED,
  251. ]);
  252. }
  253. }