Bez popisu

Order.php 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2017/12/5
  6. * Time: 15:07
  7. */
  8. namespace App;
  9. use Illuminate\Database\Eloquent\Model;
  10. use PHPExcel;
  11. use PHPExcel_Writer_Excel2007;
  12. class Order extends Model
  13. {
  14. public $timestamps = false;
  15. protected $table = "order";
  16. public static function olist(){
  17. $result = Order::orderBy('id', 'desc')->get();
  18. return json_decode(json_encode($result),true);
  19. }
  20. /**
  21. * @param old_skus 只用于更新订单,计算sku变化
  22. * @param new_skus
  23. * @param flag 1;录入,加占库存 -1:到卖家或删除,减占库存
  24. */
  25. public static function diffSkuToRedis($old_skus, $new_skus, $flag=1){
  26. $new_arr = array();
  27. foreach($new_skus as $k=>$v){
  28. if($flag == 1){
  29. $new_arr[$v['sku_id']] = $v['num'];
  30. }elseif($flag == -1){
  31. $new_arr[$v['sku_id']] = 0-$v['num'];
  32. }
  33. }
  34. if(!empty($old_skus)){
  35. $old_arr = array();
  36. foreach($old_skus as $k=>$v){
  37. $old_arr[$v['sku_id']] = $v['num'];
  38. }
  39. $res_arr = array();
  40. foreach($new_arr as $sku_id=>$num){
  41. if(isset($old_arr[$sku_id])){
  42. if($old_arr[$sku_id] != $num){
  43. $diff_num = $num - $old_arr[$sku_id];
  44. unset($old_arr[$sku_id]);
  45. }else{
  46. unset($old_arr[$sku_id]);
  47. continue;
  48. }
  49. }else{
  50. $diff_num = $num;
  51. }
  52. $res_arr[$sku_id] = $diff_num;
  53. }
  54. if(!empty($old_arr)){
  55. foreach($old_arr as $k=>&$old_num){
  56. $old_num = 0 - $old_num;
  57. }
  58. $res_arr = $res_arr + $old_arr;
  59. }
  60. }else{
  61. $res_arr = $new_arr;
  62. }
  63. $redis_table = config('constants.SKU_QUANTITY_TABLE');
  64. foreach($res_arr as $key => $val){
  65. //redis 存货号
  66. $code = GoodsSkus::where('id', $key)->pluck('code');
  67. $sku_redis_val = RedisModel::hGet($redis_table, $code);
  68. $sku_up_val = $sku_redis_val>0 ? ($sku_redis_val + $val) : $val;
  69. RedisModel::hSet($redis_table, $code, $sku_up_val);
  70. }
  71. }
  72. /**
  73. * 添加订单
  74. */
  75. public static function mjOrderAdd($orderList){
  76. $orderList["method"] = "maijiayun.order.add";
  77. //status格式
  78. $staus_arr = array(
  79. 0 => '待付款',
  80. 1 => '已付款待审核',
  81. 2 => '已审核待发货',
  82. 3 => '已发货',
  83. );
  84. $orderList['status'] = $staus_arr[$orderList['status']];
  85. $result = self::mjApi($orderList);
  86. return $result;
  87. }
  88. /**
  89. * 更新订单
  90. */
  91. public static function mjOrderUpdate($orderList){
  92. $orderList["method"] = "maijiayun.order.update";
  93. $staus_arr = array(
  94. 0 => '待付款',
  95. 1 => '已付款待审核',
  96. 2 => '已审核待发货',
  97. 3 => '已发货',
  98. 4 => '已取消',
  99. );
  100. $orderList['status'] = $staus_arr[$orderList['status']];
  101. $result = self::mjApi($orderList);
  102. return $result;
  103. }
  104. /**
  105. * 取消订单
  106. */
  107. public static function mjOrderDel($orderList){
  108. $orderList["method"] = "maijiayun.order.update";
  109. $orderList['status'] = '已取消';
  110. $result = self::mjApi($orderList);
  111. return $result;
  112. }
  113. /**
  114. * 获取订单
  115. */
  116. public static function mjOrderGet($order){
  117. $order["method"] = "maijiayun.order.get";
  118. $result = self::mjApi($order);
  119. return $result;
  120. }
  121. /**
  122. * 添加Erp商品
  123. */
  124. public static function mjErpGoodsAdd($goods){
  125. $goods["method"] = "maijiayun.goods.sku.add";
  126. $result = self::mjApi($goods);
  127. return $result;
  128. }
  129. /**
  130. * 添加商品
  131. */
  132. public static function mjGoodsAdd($goods){
  133. $goods["method"] = "maijiayun.eshop.goods.add";
  134. $result = self::mjApi($goods);
  135. return $result;
  136. }
  137. /**
  138. * 更新商品
  139. */
  140. public static function mjGoodsUpdate($goods){
  141. $goods["method"] = "maijiayun.eshop.goods.update";
  142. $result = self::mjApi($goods);
  143. return $result;
  144. }
  145. /**
  146. * 更新erp商品
  147. */
  148. public static function mjErpGoodsUpdate($goods){
  149. $goods["method"] = "maijiayun.goods.update";
  150. $result = self::mjApi($goods);
  151. return $result;
  152. }
  153. /**
  154. * 批量添加sku
  155. */
  156. public static function mjBatchSkuAdd($sku){
  157. $sku["method"] = "maijiayun.eshop.sku.batchAdd"; //批量添加
  158. //$sku["method"] = "maijiayun.eshop.sku.add";
  159. $result = self::mjApi($sku);
  160. return $result;
  161. }
  162. /**
  163. * 添加单个sku
  164. */
  165. public static function mjSkuAdd($sku){
  166. $sku["method"] = "maijiayun.eshop.sku.add";
  167. $result = self::mjApi($sku);
  168. return $result;
  169. }
  170. /**
  171. * 修改Erp sku
  172. */
  173. public static function mjErpSkuUpdate($sku){
  174. $sku["method"] = "maijiayun.goods.sku.update";
  175. $result = self::mjApi($sku);
  176. return $result;
  177. }
  178. /**
  179. * 修改shop sku
  180. */
  181. public static function mjSkuUpdate($sku){
  182. $sku["method"] = "maijiayun.eshop.sku.update";
  183. $result = self::mjApi($sku);
  184. return $result;
  185. }
  186. /**
  187. * 获取 sku 库存
  188. */
  189. public static function mjWarehouseSkuGet($sku){
  190. $sku["method"] = "maijiayun.warehouse.sku.list";
  191. $result = self::mjApi($sku);
  192. return $result;
  193. }
  194. public static function mjApi($params = array()){
  195. $accessKey = "B1E69297B5DA44DAB35099A5F28F41D9"; //erp 生成的 accessKey
  196. $accessSecret = "4sN2LbylhOglelMP";
  197. // $accessKey = "F82A757089D84F89B1782EDC3A4DA82A"; //erp 生成的 accessKey
  198. // $accessSecret = "wB2ueW8eQOHXNWcN";
  199. $params["timestamp"] = time()."000";
  200. $params["version"] = "v1";
  201. $reqparams = $params;
  202. ksort($reqparams);
  203. $sign = "";
  204. foreach($reqparams as $k=>$val){
  205. if(is_array($val)){
  206. $val = json_encode($val,320);
  207. }
  208. $sign.=$k.$val;
  209. }
  210. $params["accessKey"] = $accessKey;
  211. $params["token"] = strtoupper(sha1($accessKey.$sign.$accessSecret));
  212. $param = json_encode($params);
  213. $ch = curl_init();
  214. $headers = array("Content-type:application/json;charset='utf-8'","Accept:application/json", "Cache-Control: no-cache", "Pragma: no-cache");
  215. curl_setopt($ch, CURLOPT_URL, "https://api.erp.maijiayun.cn"); //api 地址
  216. curl_setopt($ch, CURLOPT_POST, 1);
  217. curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  218. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  219. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  220. curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
  221. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  222. $response = curl_exec($ch);
  223. curl_close($ch);
  224. $response = json_decode($response, true);
  225. Log::errorLog($params,['mjApi_response'=>$response],"order/mjApi",1,'');
  226. if (!$response || !is_array($response)) {
  227. return false;
  228. }
  229. if (!array_key_exists("isOk", $response)) {
  230. return false;
  231. } elseif( $response['isOk'] == false){
  232. // dd($response);
  233. Log::errorLog($params,['mjApi_response'=>$response],"order/mjApi",0,'');
  234. return false;
  235. } else {
  236. return $response;
  237. }
  238. }
  239. # 生成外部订单号
  240. public static function createOuterCode(){
  241. $prefix = config('constants.ORDER_PREFIX');
  242. $order_sn = $prefix.rand(100, 9999). substr(time(), 5, 5). rand(10, 9999);
  243. return $order_sn;
  244. }
  245. /**
  246. * 导出excel
  247. * @param $data
  248. * @param string
  249. */
  250. public static function export_excel($data, $filename = '未命名.xlsx', $indexKey, $title) {
  251. if( !is_array($indexKey)) return false;
  252. $header_arr = array('A','B','C','D','E','F','G','H','I','J','K','L','M', 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
  253. //初始化PHPExcel()
  254. $objPHPExcel = new PHPExcel();
  255. $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
  256. //接下来就是写数据到表格里面去
  257. $objActSheet = $objPHPExcel->getActiveSheet();
  258. foreach($title as $k=>$item){
  259. $objActSheet->setCellValue($header_arr[$k].'1',$item);
  260. }
  261. $startRow = 2;
  262. foreach ($data as $row) {
  263. foreach ($indexKey as $key => $value){
  264. //这里是设置单元格的内容
  265. $objActSheet->setCellValue($header_arr[$key].$startRow,$row[$value]);
  266. }
  267. $startRow++;
  268. }
  269. header("Pragma: public");
  270. header("Expires: 0");
  271. header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
  272. header("Content-Type:application/force-download");
  273. header("Content-Type:application/vnd.ms-execl");
  274. header("Content-Type:application/octet-stream");
  275. header("Content-Type:application/download");;
  276. header('Content-Disposition:attachment;filename='.$filename.'');
  277. header("Content-Transfer-Encoding:binary");
  278. $objWriter->save('php://output');
  279. exit();
  280. }
  281. }