| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 | <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');require_once APPPATH.'/models/Model_fullorder.php';require_once APPPATH.'/models/Model_fullordersmt.php';require_once APPPATH.'/models/Model_warehouse.php';require_once APPPATH.'/models/Model_headorder.php';require_once APPPATH.'/models/Model_typeclass.php';class Order_operation{	var $ci;    public function __construct()    {      		$this->ci = &get_instance();		$this->fullorder=new Model_fullorder();		$this->fullordersmt=new Model_fullordersmt();		$this->warehouse=new Model_warehouse();		$this->headorder=new Model_headorder();		$this->typeclass=new Model_typeclass();		$this->ci->load->library('order_process');    }    /***     * action str      * order model order     */    public function handle($action,$ordernumber){        switch($action){            case "add":               return  $this->add($ordernumber);            break;            case "update":                $this->update($ordernumber);            break;            case "delete":                $this->delete($ordernumber);            break;        }    }     public function add($orderinfo){		$where='1=1';        try {            $thisdata=$this->fullorder->find('orderinfo='.$orderinfo);            if (!$thisdata) {                $thisdata=$this->fullordersmt->find('orderinfo='.$orderinfo);            }            if (!$thisdata) {                return false;			}            $this->ci->db->trans_begin();            $headOrder_id=$this->headorder->setOrder($thisdata);//model insert            if (!$headOrder_id) {               throw new Exception('something wrong');			}            $whlable=explode('|', trim($thisdata['whlabel'], "|"));            $whlable=array_map(function ($v) {                return substr($v, 0, (strpos($v, '-')));            }, $whlable);            if (stripos($thisdata['fpdata'], ';') !== false) {                $fpdata = explode(';', rtrim($thisdata['fpdata'], ';'));                foreach ($fpdata as $k=>$v) {                    $fpdata[$k] = explode('|', $v);                }            }            $typeClass=$this->typeclass->find_all();            $n=1;            foreach ($fpdata as $k=>$fp) {                if (stripos($fp[0], '128')>-1||stripos($fp[1], 'Wigs')>-1) {//头套                    $sku=$this->sortsku($typeClass, $fp);                    $skustr="";                    foreach ($sku as $v) {                        if (isset($v['zh'])&&!empty($v['zh'])) {                            $skustr.=$v['zh'].' ';                        }                    }                    $i=$fp[2];                    for ($i=$fp[2];$i>0;$i--) {                        $orders[]=[                            'fpdata'=>$fp[0],                            'warehouse'=>$thisdata['warehouse'],                            'whlabel'=>$whlable[$k],                            'number'=>$thisdata['number'].'-'.$n,                            'orderinfo'=>$thisdata['orderinfo'].'-'.$n,                            'p_order'=>$thisdata['orderinfo'],                            'p_number'=>$thisdata['number'],                            'orderremarks'=>$thisdata['orderremarks'],//订单品名                            'product'=>$thisdata['product'],                            'sku'=>json_encode($sku),                            'shipremarks'=>$skustr,                        ];                        $n++;                    }                }            }            if (!empty($orders)) {				foreach ($orders as $order) {					$process=$this->ci->order_process->createOrderProcess($order);				}			}            if ($this->ci->db->trans_status() == false) {               throw new Exception('something wrong!');			}			$this->ci->db->trans_commit();			return true;        		}catch(Exception  $e){			$this->ci->db->trans_rollback();			return false;		}	}	public function update($orderinfo){		$flag=false;		$where='1=1';		// $orderinfo=22007131647400;		$thisdata=$this->fullorder->find('orderinfo='.$orderinfo);		if(!$thisdata){			$thisdata=$this->fullordersmt->find('orderinfo='.$orderinfo);		}		if(!$thisdata){			return false;		}		$query=$this->ci->db->select('a.whlabel,a.p_order,count(a.whlabel) as num')		->from('headorder_item as a')		->join('process as b',' a.`id`=b.`order_id`','inner')		->join('flow_status as c','b.`status_id`=c.id')		->where('a.p_order='.$orderinfo)		->where('b.state="'.Model_Process::STATUS_PROCESSING.'"')		->group_by('a.whlabel')		->order_by('a.whlabel ASC','c.order ASC')		->get();		$headOrder=$query->result_array();		$headOrder=array_combine(array_column($headOrder,'whlabel'),$headOrder);		$whlable=explode('|',trim($thisdata['whlabel'],"|"));		$whlable=array_map(function($v){				return substr($v,0,(strpos($v,'-')));		},$whlable);		if(stripos($thisdata['fpdata'],';') !== false)		{			$fpdata = explode(';',rtrim($thisdata['fpdata'],';'));		    foreach ($fpdata as $k=>$v) 		    {			    $fpdata[$k] = explode('|',$v);            }		}		$n=$this->ci->db			->from('headorder_item')			->where('p_order='.$orderinfo)			->count_all_results();		$typeClass=$this->typeclass->find_all();        foreach ($fpdata as $k=>$fp) {            if(stripos($fp[0],'128')>-1||stripos($fp[1],'Wigs')>-1){//头套				$label=$whlable[$k]?$whlable[$k]:'';				if(!isset($headOrder[$label])||empty($headOrder[$label])){//新增					$add_num=$fp[2];					$this->additem($fp,$typeClass,$thisdata,$add_num,$label,$n);					$n+=$add_num;				}else{					$c_num=$fp[2];					$l_num=$headOrder[$label]['num'];					//比较数量 多 增					if($c_num>$l_num){						$add_num=$c_num-$l_num;						$this->additem($fp,$typeClass,$thisdata,$add_num,$label,$n);						$n+=$add_num;					}elseif($c_num<$l_num){						$del_num=$l_num-$c_num;						$this->delitem($label,$del_num);					}else{//不做任何操作						continue;					}				}        	}								}		return true;	}	private function additem($fp,$typeClass,$thisdata,$add_num,$label,$n){		$sku=$this->sortsku($typeClass,$fp);		$skustr="";		foreach($sku as $v){			if(isset($v['zh'])&&!empty($v['zh'])){				$skustr.=$v['zh'].' ';			}		}		for($i=$n;$i<$n+$add_num+1;$i++){			$n+=1;			$orders[]=[				'fpdata'=>$fp[0],				'warehouse'=>$thisdata['warehouse'],				'whlabel'=>$label,				'number'=>$thisdata['number'].'-'.$n,				'orderinfo'=>$thisdata['orderinfo'].'-'.$n,				'p_order'=>$thisdata['orderinfo'],				'p_number'=>$thisdata['number'],				'orderremarks'=>$thisdata['orderremarks'],//订单品名				'product'=>$thisdata['product'],				'sku'=>json_encode($sku),				'shipremarks'=>$skustr,			];		}		//addprocess 完成		foreach($orders as $order){			$this->ci->order_process->createOrderProcess($order);		}		return true;	}	private function delitem($label,$num){		$query=$this->ci->db->select('a.id,a.whlabel,a.p_order,b.id as process_id,c.code,c.name,c.order')		->from('headorder_item as a')		->join('process as b',' a.`id`=b.`order_id`','inner')		->join('flow_status as c','b.`status_id`=c.id')		->where('a.whlabel='.$label)			->where('b.state="'.Model_Process::STATUS_PROCESSING.'"')		->order_by('c.order ASC')		->limit($num)		->get();		$del_items=$query->result_array();		return	$this->db->where_in('id',array_column($del_items,'process_id'))->update('process',[			'state'=>Model_Process::STATUS_CANCELED,		]);	}    private function sortsku($typeClass,$fp){		$arr = explode(',',$fp[0]);		$length=$arr[0];		$arr1=explode('-',$arr[1]);		array_push($arr1,$length);		$ids=array_column($typeClass,'id');		$typeClass=array_combine($ids,$typeClass);				foreach($arr1 as $v){			if(!empty($v)&&isset($typeClass[$v])&&!empty($typeClass[$v])){				$sku[$v]=$typeClass[$v];			}		}		// $cptt = array(16=>128,18=>'',6=>30,10=>72,9=>'');//头套		$pm = array(13=>'',22=>'',8=>'',15=>'',18=>'','100'=>'','dc'=>'','c'=>'',14=>'',12=>'',25=>'',26=>'',27=>'',10=>'',6=>'',9=>'',999=>'',9999=>'');//品名顺序		$res=[];		$sku=array_combine(array_column($sku,'classid'),$sku);		foreach ($pm as $k => $v) {			if(isset($sku[$k])&&!empty($sku[$k])){				$res[]=$sku[$k];			}		}		return $res;	}	public function delete($orderinfo){		$query=$this->ci->db->select('a.id,a.whlabel,a.p_order,b.id as process_id,c.code,c.name,c.order')		->from('headorder_item as a')		->join('process as b',' a.`id`=b.`order_id`','inner')		->join('flow_status as c','b.`status_id`=c.id')		->where('a.p_order='.$orderinfo)		->where('b.state="'.Model_Process::STATUS_PROCESSING.'"')		->order_by('c.order ASC')		->get();		$del_items=$query->result_array();		return	$this->db->where_in('id',array_column($del_items,'process_id'))->update('process',[			'state'=>Model_Process::STATUS_CANCELED,		]);	}}	
 |