StatSaleClass.php 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341
  1. <?php
  2. namespace bizGhs\stat\classes;
  3. use biz\stat\classes\StatCgClass;
  4. use biz\stat\classes\StatStockInClass;
  5. use biz\stat\classes\StatStockOutClass;
  6. use bizGhs\cg\classes\CgRefundClass;
  7. use bizGhs\custom\classes\CustomClass;
  8. use bizGhs\expend\classes\ExpendClass;
  9. use bizGhs\item\classes\ItemClassClass;
  10. use bizGhs\order\classes\OrderItemClass;
  11. use bizGhs\order\classes\PurchaseOrderClass;
  12. use bizGhs\order\classes\PurchaseOrderItemClass;
  13. use bizGhs\order\classes\RefundOrderClass;
  14. use bizGhs\order\classes\StockWastageOrderClass;
  15. use bizGhs\product\classes\ProductClass;
  16. use bizGhs\staff\classes\SalaryClass;
  17. use bizHd\goods\classes\PlantCgClass;
  18. use bizHd\order\classes\OrderClass as HdOrderClass;
  19. use bizGhs\order\classes\OrderClass;
  20. use bizHd\purchase\classes\PurchaseClass;
  21. use bizHd\refund\classes\HdRefundClass;
  22. use common\components\arrayUtil;
  23. use common\components\dateUtil;
  24. use common\components\dict;
  25. use common\components\noticeUtil;
  26. use common\components\util;
  27. use Yii;
  28. use bizGhs\base\classes\BaseClass;
  29. class StatSaleClass extends BaseClass
  30. {
  31. public static $baseFile = '\bizGhs\stat\models\StatSale';
  32. public static function wastage($mainId)
  33. {
  34. $get = Yii::$app->request->get();
  35. $where = [];
  36. $where['mainId'] = $mainId;
  37. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  38. $startTime = $get['startTime'] ?? '';
  39. $endTime = $get['endTime'] ?? '';
  40. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  41. $start = $period['startTime'];
  42. $end = $period['endTime'];
  43. $currentStartDate = date('Y-m-d', strtotime($start));
  44. $currentEndDate = date('Y-m-d', strtotime($end));
  45. $currentStartTime = $currentStartDate . ' 00:00:00';
  46. $currentEndTime = $currentEndDate . ' 23:59:59';
  47. $where['addTime'] = ['between', [$currentStartTime, $currentEndTime]];
  48. $list = StockWastageOrderClass::getAllByCondition($where, null, '*');
  49. $totalPrice = 0;
  50. if (!empty($list)) {
  51. foreach ($list as $order) {
  52. $price = $order['price'] ?? 0;
  53. $totalPrice = bcadd($totalPrice, $price, 2);
  54. }
  55. }
  56. $totalPrice = floatval($totalPrice);
  57. return ['totalPrice' => $totalPrice];
  58. }
  59. //结账跟踪 ssh 20230503
  60. public static function OrderClear($mainId)
  61. {
  62. $get = Yii::$app->request->get();
  63. $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  64. $timeStyle = $get['timeStyle'] ?? 0;
  65. if (isset($get['searchTime']) && !empty($get['searchTime'])) {
  66. $searchTime = $get['searchTime'];
  67. $startTime = $get['startTime'] ?? '';
  68. $endTime = $get['endTime'] ?? '';
  69. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  70. $start = $period['startTime'];
  71. $end = $period['endTime'];
  72. $currentStartDate = date('Y-m-d', strtotime($start));
  73. $currentEndDate = date('Y-m-d', strtotime($end));
  74. $currentStartTime = $currentStartDate . ' 00:00:00';
  75. $currentEndTime = $currentEndDate . ' 23:59:59';
  76. if ($timeStyle == 0) {
  77. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  78. } else {
  79. $where['clearTime'] = ['between', [$currentStartTime, $currentEndTime]];
  80. }
  81. } else {
  82. $payTime = 'today';
  83. $startTime = $get['startTime'] ?? '';
  84. $endTime = $get['endTime'] ?? '';
  85. $period = dateUtil::formatTime($payTime, $startTime, $endTime, true);
  86. $start = $period['startTime'];
  87. $end = $period['endTime'];
  88. $currentStartDate = date('Y-m-d', strtotime($start));
  89. $currentEndDate = date('Y-m-d', strtotime($end));
  90. $currentStartTime = $currentStartDate . ' 00:00:00';
  91. $currentEndTime = $currentEndDate . ' 23:59:59';
  92. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  93. }
  94. $customId = $get['customId'] ?? 0;
  95. if (!empty($customId)) {
  96. $where['customId'] = $customId;
  97. }
  98. $unClear = $get['unClear'] ?? 0;
  99. if ($unClear == 1) {
  100. $where['clearId'] = 0;
  101. $where['remainDebtPrice>'] = 0;
  102. }
  103. $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
  104. return ['list' => $ghsOrderList];
  105. }
  106. //客户下单榜 ssh 20230502
  107. public static function statCustomCg($mainId)
  108. {
  109. $get = Yii::$app->request->get();
  110. $where = [];
  111. $where['mainId'] = $mainId;
  112. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  113. $startTime = $get['startTime'] ?? '';
  114. $endTime = $get['endTime'] ?? '';
  115. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  116. $start = $period['startTime'];
  117. $end = $period['endTime'];
  118. $where['time'] = ['between', [$start, $end]];
  119. $currentStartDate = date('Y-m-d', strtotime($start));
  120. $currentEndDate = date('Y-m-d', strtotime($end));
  121. $currentStartTime = $currentStartDate . ' 00:00:00';
  122. $currentEndTime = $currentEndDate . ' 23:59:59';
  123. $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  124. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  125. $customList = CustomClass::getAllByCondition(['ownMainId' => $mainId], null, 'id,name,py', 'id');
  126. $arr = [];
  127. $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
  128. $totalAmount = 0;
  129. $totalNum = 0;
  130. if (!empty($ghsOrderList)) {
  131. foreach ($ghsOrderList as $ghsKey => $ghsOrder) {
  132. $actPrice = $ghsOrder['actPrice'] ?? 0;
  133. $orderPrice = $ghsOrder['orderPrice'] ?? 0;
  134. $tkPrice = $ghsOrder['tkPrice'] ?? 0;
  135. $customId = $ghsOrder['customId'] ?? 0;
  136. $status = $ghsOrder['status'] ?? 0;
  137. $book = $ghsOrder['book'] ?? 0;
  138. if ($status == 1 || $status == 5) {
  139. continue;
  140. }
  141. if ($book == 1 && $status == 2) {
  142. continue;
  143. }
  144. $customName = $customList[$customId]['name'] ?? '';
  145. $py = $customList[$customId]['py'] ?? '';
  146. $totalNum++;
  147. $totalAmount = bcadd($totalAmount, $actPrice, 2);
  148. if (isset($arr[$customId])) {
  149. $arr[$customId]['num']++;
  150. $arr[$customId]['amount'] = bcadd($arr[$customId]['amount'], $actPrice, 2);
  151. $arr[$customId]['orderPrice'] = bcadd($arr[$customId]['orderPrice'], $orderPrice, 2);
  152. $arr[$customId]['tkPrice'] = bcadd($arr[$customId]['tkPrice'], $tkPrice, 2);
  153. } else {
  154. $arr[$customId] = [
  155. 'customId' => $customId,
  156. 'customName' => $customName,
  157. 'py' => $py,
  158. 'num' => 1,
  159. 'amount' => $actPrice,
  160. 'levelName' => '',
  161. 'orderPrice' => $orderPrice,
  162. 'tkPrice' => $tkPrice,
  163. ];
  164. }
  165. }
  166. $arr = arrayUtil::arraySort($arr, 'amount');
  167. }
  168. $totalAmount = floatval($totalAmount);
  169. return ['list' => $arr, 'totalAmount' => $totalAmount, 'totalNum' => $totalNum];
  170. }
  171. public static function profile($mainId)
  172. {
  173. //收入:批发开单、零售开单(含门店、美团和微信客服)、出库
  174. //支出:采购、调拨入库、批发退款、零售退款、店租、水电、伙食
  175. $get = Yii::$app->request->get();
  176. $where = [];
  177. $where['mainId'] = $mainId;
  178. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  179. $startTime = $get['startTime'] ?? '';
  180. $endTime = $get['endTime'] ?? '';
  181. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  182. $start = $period['startTime'];
  183. $end = $period['endTime'];
  184. $where['time'] = ['between', [$start, $end]];
  185. $currentStartDate = date('Y-m-d', strtotime($start));
  186. $currentEndDate = date('Y-m-d', strtotime($end));
  187. $currentStartTime = $currentStartDate . ' 00:00:00';
  188. $currentEndTime = $currentEndDate . ' 23:59:59';
  189. //出库
  190. $stockOut = StatStockOutClass::getSum($where);
  191. //采购入库
  192. $cgWhere = ['mainId' => $mainId, 'status' => 4];
  193. $cgWhere['entryTime'] = ['between', [$currentStartTime, $currentEndTime]];
  194. $cgList = PurchaseOrderClass::getAllByCondition($cgWhere, null, '*');
  195. $cg = 0;
  196. if (!empty($cgList)) {
  197. foreach ($cgList as $cgKey => $cgItem) {
  198. $actPrice = $cgItem['actPrice'] ?? 0;
  199. $paidPrice = $cgItem['paidPrice'] ?? 0;
  200. $total = bcadd($actPrice, $paidPrice, 2);
  201. //物流费用不算成本
  202. if (isset($cgItem['isCost']) && $cgItem['isCost'] == 0) {
  203. $shortCharge = $cgItem['shortCharge'] ?? 0;
  204. $longCharge = $cgItem['longCharge'] ?? 0;
  205. $pickCharge = $cgItem['pickCharge'] ?? 0;
  206. $localCharge = $cgItem['localCharge'] ?? 0;
  207. $a1 = bcadd($shortCharge, $longCharge, 2);
  208. $a2 = bcadd($pickCharge, $localCharge, 2);
  209. $a = bcadd($a1, $a2, 2);
  210. $total = bcsub($total, $a, 2);
  211. }
  212. $cg = bcadd($cg, $total, 2);
  213. }
  214. }
  215. $cg = floatval($cg);
  216. $lsCgWhere = ['mainId' => $mainId, 'status' => 4,'belongCost'=>1];
  217. $lsCgWhere['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  218. $lsCgList = PurchaseClass::getAllByCondition($lsCgWhere, null, '*');
  219. $lsCgAmount = 0;
  220. if (!empty($lsCgList)) {
  221. foreach ($lsCgList as $lsCgItem) {
  222. $actPrice = $lsCgItem['actPrice'] ?? 0;
  223. $lsCgAmount = bcadd($lsCgAmount, $actPrice, 2);
  224. }
  225. }
  226. $lsCgAmount = floatval($lsCgAmount);
  227. $lzWhere = ['mainId' => $mainId, 'status' => 3];
  228. $lzWhere['entryTime'] = ['between', [$currentStartTime, $currentEndTime]];
  229. $lzCgList = PlantCgClass::getAllByCondition($lzWhere, null, '*');
  230. $lzCgAmount = 0;
  231. if (!empty($lzCgList)) {
  232. foreach ($lzCgList as $lzInfo) {
  233. $actPrice = $lzInfo['actPrice'] ?? 0;
  234. $lzCgAmount = bcadd($actPrice, $lzCgAmount, 2);
  235. }
  236. }
  237. $lzCgAmount = floatval($lzCgAmount);
  238. //调拨入库
  239. $stockIn = StatStockInClass::getSum($where);
  240. $totalOrderNum = 0;
  241. //批发开单
  242. $pfIncome = 0;
  243. $pfSendCost = 0;
  244. $pfPack = 0;
  245. $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  246. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  247. $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
  248. if (!empty($ghsOrderList)) {
  249. foreach ($ghsOrderList as $ghsKey => $ghsOrder) {
  250. $actPrice = $ghsOrder['actPrice'] ?? 0;
  251. $sendCost = $ghsOrder['sendCost'] ?? 0;
  252. $refundSendCost = $ghsOrder['refundSendCost'] ?? 0;
  253. $remainSendCost = bcsub($sendCost, $refundSendCost, 2);
  254. $packCost = $ghsOrder['packCost'] ?? 0;
  255. $refundPackCost = $ghsOrder['refundPackCost'] ?? 0;
  256. $remainPackCost = bcsub($packCost, $refundPackCost, 2);
  257. $pfIncome = bcadd($pfIncome, $actPrice, 2);
  258. $pfSendCost = bcadd($pfSendCost, $remainSendCost, 2);
  259. $pfPack = bcadd($pfPack, $remainPackCost, 2);
  260. $totalOrderNum++;
  261. }
  262. }
  263. //零售开单
  264. $lsIncome = 0;
  265. $lsSendCost = 0;
  266. $lsServiceFee = 0;
  267. $hdWhere = ['mainId' => $mainId, 'status' => ['in', [HdOrderClass::ORDER_STATUS_UN_SEND, HdOrderClass::ORDER_STATUS_SENDING, HdOrderClass::ORDER_STATUS_COMPLETE]]];
  268. $hdWhere['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  269. $hdOrderList = HdOrderClass::getAllByCondition($hdWhere, null, '*');
  270. foreach ($hdOrderList as $hdKey => $hdOrder) {
  271. $actPrice = $hdOrder['mainPay'] ?? 0;
  272. if ($hdOrder['tkPrice'] && $hdOrder['tkPrice'] > 0) {
  273. $actPrice = bcsub($actPrice, $hdOrder['tkPrice'], 2);
  274. }
  275. $cash = $hdOrder['cash'] ?? 0;
  276. $serviceFee = $hdOrder['serviceFee'] ?? 0;
  277. $sendCost = $hdOrder['sendCost'] ?? 0;
  278. $lsIncome = bcadd($actPrice, $lsIncome, 2);
  279. $lsIncome = bcadd($lsIncome, $cash, 2);
  280. //退款取消的先不计算成本
  281. if ($actPrice > 0) {
  282. $lsSendCost = bcadd($lsSendCost, $sendCost, 2);
  283. $lsServiceFee = bcadd($lsServiceFee, $serviceFee, 2);
  284. }
  285. $totalOrderNum++;
  286. }
  287. $condition = ['mainId' => $mainId, 'addTime' => ['between', [$currentStartTime, $currentEndTime]],];
  288. $salary = 0;
  289. $salaryList = SalaryClass::getAllByCondition($condition, null, '*', null, true);
  290. if (!empty($salaryList)) {
  291. foreach ($salaryList as $item) {
  292. $salary = bcadd($salary, $item->amount, 2);
  293. }
  294. }
  295. $condition = ['mainId' => $mainId, 'payTime' => ['between', [$currentStartTime, $currentEndTime]],];
  296. $expendList = ExpendClass::getAllByCondition($condition, null, '*', null, true);
  297. $expendTypeMap = dict::getDict('expendTypeMap');
  298. $expendAmount = [];
  299. if (!empty($expendList)) {
  300. foreach ($expendList as $item) {
  301. $type = $item->type ?? 0;
  302. $amount = $item->amount ?? 0;
  303. $name = $expendTypeMap[$type] ?? '暂无';
  304. if (isset($expendAmount[$type]['amount']) == false) {
  305. $expendAmount[$type]['amount'] = 0;
  306. }
  307. $expendAmount[$type]['name'] = $name;
  308. $expendAmount[$type]['amount'] = floatval(bcadd($expendAmount[$type]['amount'], $amount, 2));
  309. }
  310. }
  311. $income = [
  312. ['name' => '批发', 'id' => 'pf', 'amount' => floatval($pfIncome)],
  313. ['name' => '零售', 'id' => 'ls', 'amount' => floatval($lsIncome)],
  314. ['name' => '调拨出库', 'id' => 'allot', 'amount' => floatval($stockOut)],
  315. ];
  316. $expend = [
  317. ['name' => '批发运费', 'id' => 'pfSendCost', 'amount' => floatval($pfSendCost)],
  318. ['name' => '批发包装费', 'id' => 'pfSendCost', 'amount' => floatval($pfPack)],
  319. ['name' => '零售运费', 'id' => 'lsSendCost', 'amount' => floatval($lsSendCost)],
  320. ['name' => '美团手续费', 'id' => 'mtFee', 'amount' => floatval($lsServiceFee)],
  321. ['name' => '花材采购', 'id' => 'cg', 'amount' => floatval($cg)],
  322. ['name' => '绿植资材采购', 'id' => 'cg', 'amount' => floatval($lzCgAmount)],
  323. ['name' => '零售花材采购', 'id' => 'cg', 'amount' => floatval($lsCgAmount)],
  324. ['name' => '调拨入库', 'id' => 'allot', 'amount' => floatval($stockIn)],
  325. ['name' => '员工工资', 'id' => 'salary', 'amount' => floatval($salary)],
  326. ];
  327. if (!empty($expendAmount)) {
  328. foreach ($expendAmount as $it) {
  329. $expend[] = $it;
  330. }
  331. }
  332. $balance = 0;
  333. foreach ($income as $it) {
  334. $amount = $it['amount'] ?? 0;
  335. $balance = bcadd($amount, $balance, 2);
  336. }
  337. foreach ($expend as $it) {
  338. $amount = $it['amount'] ?? 0;
  339. $balance = bcsub($balance, $amount, 2);
  340. }
  341. return ['income' => $income, 'expend' => $expend, 'balance' => floatval($balance), 'totalOrderNum' => $totalOrderNum];
  342. }
  343. //按花材统计收入 ssh 20231111
  344. public static function incomeOutItem($mainId)
  345. {
  346. $get = Yii::$app->request->get();
  347. $export = $get['export'] ?? 0;
  348. $where = [];
  349. $where['mainId'] = $mainId;
  350. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  351. $startTime = $get['startTime'] ?? '';
  352. $endTime = $get['endTime'] ?? '';
  353. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  354. $start = $period['startTime'];
  355. $end = $period['endTime'];
  356. $where['time'] = ['between', [$start, $end]];
  357. $currentStartDate = date('Y-m-d', strtotime($start));
  358. $currentEndDate = date('Y-m-d', strtotime($end));
  359. $currentStartTime = $currentStartDate . ' 00:00:00';
  360. $currentEndTime = $currentEndDate . ' 23:59:59';
  361. $s = strtotime($currentStartTime);
  362. $e = strtotime($currentEndTime);
  363. $p = bcsub($e, $s);
  364. $xx = bcmul(86400, 90);
  365. if ($p > $xx) {
  366. util::fail('查询时间不能超过三个月');
  367. }
  368. $productList = ProductClass::getAllByCondition(['mainId' => $mainId], null, 'id,name', 'id');
  369. if (empty($productList)) {
  370. util::fail('没有找到花材');
  371. }
  372. $arr = [];
  373. //打包费和运费
  374. $packFreight = 0;
  375. $onlyRefundCg = 0;
  376. $onlyRefund = 0;
  377. $totalDiscountAmount = 0;
  378. $totalLabourCost = 0;
  379. //采购入库
  380. $tbCgIncome = [];
  381. $tbCgIncomeStaff = [];
  382. $cgWhere = ['mainId' => $mainId, 'status' => 4];
  383. $cgWhere['entryTime'] = ['between', [$currentStartTime, $currentEndTime]];
  384. $cgList = PurchaseOrderClass::getAllByCondition($cgWhere, null, '*');
  385. if (!empty($cgList)) {
  386. foreach ($cgList as $cgKey => $cgItem) {
  387. $packingCharge = $cgItem['packingCharge'] ?? 0;
  388. $shortCharge = $cgItem['shortCharge'] ?? 0;
  389. $longCharge = $cgItem['longCharge'] ?? 0;
  390. $pickCharge = $cgItem['pickCharge'] ?? 0;
  391. $localCharge = $cgItem['localCharge'] ?? 0;
  392. $packFreight = bcadd($packFreight, $packingCharge, 2);
  393. $packFreight = bcadd($packFreight, $shortCharge, 2);
  394. $packFreight = bcadd($packFreight, $longCharge, 2);
  395. $packFreight = bcadd($packFreight, $pickCharge, 2);
  396. $packFreight = bcadd($packFreight, $localCharge, 2);
  397. $tkPrice = $cgItem['tkPrice'] ?? 0;
  398. $orderSn = $cgItem['orderSn'] ?? '';
  399. $cgItemInfoList = PurchaseOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  400. if (!empty($cgItemInfoList)) {
  401. foreach ($cgItemInfoList as $cgData) {
  402. $itemNum = $cgData->itemNum ?? 0;
  403. $productId = $cgData->productId ?? 0;
  404. $refundNum = $cgData->refundNum ?? 0;
  405. $remainNum = bcsub($itemNum, $refundNum);
  406. $bigPrice = $cgData->bigPrice ?? 0;
  407. $amount = bcmul($remainNum, $bigPrice, 2);
  408. $itemName = $productList[$productId]['name'] ?? '已删除';
  409. $arr[$productId]['name'] = $itemName;
  410. $arr[$productId]['id'] = $productId;
  411. if (isset($arr[$productId]['expend'])) {
  412. $arr[$productId]['expend'] = bcadd($amount, $arr[$productId]['expend'], 2);
  413. $arr[$productId]['outNum'] = bcadd($arr[$productId]['outNum'], $remainNum, 2);
  414. } else {
  415. $arr[$productId]['expend'] = $amount;
  416. $arr[$productId]['outNum'] = $remainNum;
  417. $arr[$productId]['inNum'] = 0;
  418. $arr[$productId]['income'] = 0;
  419. }
  420. }
  421. if ($tkPrice > 0) {
  422. $cgRefundOrderList = CgRefundClass::getAllByCondition(['relateOrderSn' => $orderSn], null, '*', null, true);
  423. if (!empty($cgRefundOrderList)) {
  424. foreach ($cgRefundOrderList as $cgRefundOrder) {
  425. if ($cgRefundOrder->refundType == 2) {
  426. $onlyRefundCg = bcadd($cgRefundOrder->refundPrice, $onlyRefundCg, 2);
  427. }
  428. }
  429. }
  430. }
  431. }
  432. }
  433. }
  434. //批发开单
  435. $pfIncome = 0;
  436. $tbSaleIncome = [];
  437. $tbSaleIncomeStaff = [];
  438. $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  439. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  440. $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
  441. if (!empty($ghsOrderList)) {
  442. foreach ($ghsOrderList as $ghsKey => $ghsOrder) {
  443. $book = $ghsOrder['book'] ?? 0;
  444. $status = $ghsOrder['status'] ?? 0;
  445. if ($book == 1 && $status == 2) {
  446. //预订单待发货的不算收入
  447. continue;
  448. }
  449. if ($status == 0 || $status == 5) {
  450. continue;
  451. }
  452. $shopAdminId = $ghsOrder['shopAdminId'] ?? 0;
  453. $shopAdminName = $ghsOrder['shopAdminName'] ?? '';
  454. $orderSn = $ghsOrder['orderSn'] ?? '';
  455. $actPrice = $ghsOrder['actPrice'] ?? 0;
  456. $tkPrice = $ghsOrder['tkPrice'] ?? 0;
  457. $pfIncome = bcadd($pfIncome, $actPrice, 2);
  458. $labourCost = $ghsOrder['labourCost'] ?? 0;
  459. $discountAmount = $ghsOrder['discountAmount'] ?? 0;
  460. $totalLabourCost = bcadd($labourCost, $totalLabourCost, 2);
  461. $totalDiscountAmount = bcadd($totalDiscountAmount, $discountAmount, 2);
  462. $orderItemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  463. if (!empty($orderItemList)) {
  464. foreach ($orderItemList as $orderItem) {
  465. $refundNum = $orderItem->refundNum ?? 0;
  466. $name = $orderItem->name ?? '';
  467. $productId = $orderItem->productId ?? 0;
  468. $num = $orderItem->xhNum ?? 0;
  469. $remainNum = bcsub($num, $refundNum);
  470. $xhUnitPrice = $orderItem->xhUnitPrice ?? 0;
  471. $amount = bcmul($remainNum, $xhUnitPrice, 2);
  472. $itemName = $productList[$productId]['name'] ?? '已删除';
  473. $arr[$productId]['name'] = $itemName;
  474. $arr[$productId]['id'] = $productId;
  475. if (isset($arr[$productId]['income'])) {
  476. $arr[$productId]['income'] = bcadd($amount, $arr[$productId]['income'], 2);
  477. $arr[$productId]['inNum'] = bcadd($arr[$productId]['inNum'], $remainNum, 2);
  478. } else {
  479. $arr[$productId]['expend'] = 0;
  480. $arr[$productId]['outNum'] = 0;
  481. $arr[$productId]['inNum'] = $remainNum;
  482. $arr[$productId]['income'] = $amount;
  483. }
  484. //单独统计
  485. $belongCost = $orderItem->belongCost ?? 0;
  486. if ($belongCost == 1) {
  487. if (isset($tbSaleIncome[$productId])) {
  488. $tbSaleIncome[$productId]['num'] = bcadd($tbSaleIncome[$productId]['num'], $remainNum);
  489. $tbSaleIncome[$productId]['price'] = bcadd($tbSaleIncome[$productId]['price'], $amount, 2);
  490. } else {
  491. $tbSaleIncome[$productId] = ['name' => $name, 'num' => $remainNum, 'price' => $amount];
  492. }
  493. if (isset($tbSaleIncomeStaff[$productId]) && isset($tbSaleIncomeStaff[$productId][$shopAdminId])) {
  494. $tbSaleIncomeStaff[$productId][$shopAdminId]['num'] = bcadd($tbSaleIncomeStaff[$productId][$shopAdminId]['num'], $remainNum);
  495. $tbSaleIncomeStaff[$productId][$shopAdminId]['price'] = bcadd($tbSaleIncomeStaff[$productId][$shopAdminId]['price'], $amount, 2);
  496. } else {
  497. $tbSaleIncomeStaff[$productId][$shopAdminId] = ['name' => $name, 'num' => $remainNum, 'price' => $amount, 'staffName' => $shopAdminName];
  498. }
  499. }
  500. }
  501. if ($tkPrice > 0) {
  502. $refundList = RefundOrderClass::getAllByCondition(['relateOrderSn' => $orderSn], null, '*', null, true);
  503. if (!empty($refundList)) {
  504. foreach ($refundList as $refund) {
  505. if ($refund->refundType == 1) {
  506. //退货退款前面已经考虑,这里不再处理 OK
  507. } else {
  508. $onlyRefund = bcadd($onlyRefund, $refund->refundPrice, 2);
  509. }
  510. }
  511. }
  512. }
  513. }
  514. }
  515. }
  516. //零售开单
  517. $lsIncome = 0;
  518. $hdWhere = ['mainId' => $mainId, 'status' => ['in', [HdOrderClass::ORDER_STATUS_UN_SEND, HdOrderClass::ORDER_STATUS_SENDING, HdOrderClass::ORDER_STATUS_COMPLETE]]];
  519. $hdWhere['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  520. $hdOrderList = HdOrderClass::getAllByCondition($hdWhere, null, '*');
  521. foreach ($hdOrderList as $hdKey => $hdOrder) {
  522. $actPrice = $hdOrder['mainPay'] ?? 0;
  523. $cash = $hdOrder['cash'] ?? 0;
  524. $lsIncome = bcadd($actPrice, $lsIncome, 2);
  525. $lsIncome = bcadd($lsIncome, $cash, 2);
  526. $orderSn = $hdOrder['orderSn'] ?? '';
  527. $tkPrice = $hdOrder['tkPrice'] ?? 0;
  528. $orderItemList = \bizHd\order\classes\OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  529. if (!empty($orderItemList)) {
  530. foreach ($orderItemList as $orderItem) {
  531. $unitPrice = $orderItem->unitPrice ?? 0;
  532. $refundNum = $orderItem->refundNum ?? 0;
  533. $num = $orderItem->num ?? 0;
  534. $productId = $orderItem->itemId ?? 0;
  535. $remainNum = bcsub($num, $refundNum);
  536. $amount = bcmul($remainNum, $unitPrice, 2);
  537. $itemName = $productList[$productId]['name'] ?? '已删除';
  538. $arr[$productId]['name'] = $itemName;
  539. $arr[$productId]['id'] = $productId;
  540. if (isset($arr[$productId]['income'])) {
  541. $arr[$productId]['income'] = bcadd($amount, $arr[$productId]['income'], 2);
  542. $arr[$productId]['inNum'] = bcadd($arr[$productId]['inNum'], $remainNum, 2);
  543. } else {
  544. $arr[$productId]['expend'] = 0;
  545. $arr[$productId]['outNum'] = 0;
  546. $arr[$productId]['inNum'] = $remainNum;
  547. $arr[$productId]['income'] = $amount;
  548. }
  549. }
  550. if ($tkPrice > 0) {
  551. $refundList = HdRefundClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  552. if (!empty($refundList)) {
  553. foreach ($refundList as $refund) {
  554. if ($refund->refundType == 2) {
  555. $onlyRefund = bcadd($onlyRefund, $refund->refundPrice, 2);
  556. }
  557. }
  558. }
  559. }
  560. }
  561. }
  562. $totalIncome = 0;
  563. $totalExpend = 0;
  564. if (!empty($arr)) {
  565. foreach ($arr as $key => $val) {
  566. $income = $val['income'] ?? 0;
  567. $income = floatval($income);
  568. $totalIncome = bcadd($totalIncome, $income, 2);
  569. $expend = $val['expend'] ?? 0;
  570. $expend = floatval($expend);
  571. $totalExpend = bcadd($totalExpend, $expend, 2);
  572. $profit = bcsub($income, $expend, 2);
  573. $arr[$key]['profit'] = floatval($profit);
  574. $arr[$key]['income'] = floatval($income);
  575. $arr[$key]['expend'] = floatval($expend);
  576. }
  577. }
  578. //总成本算上物流打包等费用
  579. $totalExpend = bcadd($totalExpend, $packFreight, 2);
  580. //总收入算上采购仅退款
  581. $totalIncome = bcadd($totalIncome, $onlyRefundCg, 2);
  582. //总收入加上人工费
  583. $totalIncome = bcadd($totalIncome, $totalLabourCost, 2);
  584. //总收入减掉优惠金额
  585. $totalIncome = bcsub($totalIncome, $totalDiscountAmount, 2);
  586. //总成本算上销售仅退款
  587. $totalExpend = bcadd($totalExpend, $onlyRefund, 2);
  588. $totalProfit = bcsub($totalIncome, $totalExpend, 2);
  589. $totalIncome = floatval($totalIncome);
  590. $totalExpend = floatval($totalExpend);
  591. $totalProfit = floatval($totalProfit);
  592. $arr = array_values($arr);
  593. $arr = arrayUtil::arraySort($arr, 'profit');
  594. if ($export == 1) {
  595. $statData = [
  596. ['id' => 0, 'name' => '物流打包费', 'income' => 0, 'expend' => $packFreight, 'profit' => -$packFreight, 'inNum' => 0, 'outNum' => 0],
  597. ['id' => 0, 'name' => '采购仅退款', 'income' => $onlyRefundCg, 'expend' => 0, 'profit' => $onlyRefundCg, 'inNum' => 0, 'outNum' => 0],
  598. ['id' => 0, 'name' => '销售仅退款', 'income' => 0, 'expend' => $onlyRefund, 'profit' => -$onlyRefund, 'inNum' => 0, 'outNum' => 0],
  599. ['id' => 0, 'name' => '附加人工费', 'income' => $totalLabourCost, 'expend' => 0, 'profit' => $totalLabourCost, 'inNum' => 0, 'outNum' => 0],
  600. ['id' => 0, 'name' => '优惠金额', 'income' => -$totalDiscountAmount, 'expend' => 0, 'profit' => -$totalDiscountAmount, 'inNum' => 0, 'outNum' => 0],
  601. ['id' => 0, 'name' => '【汇总金额】', 'income' => $totalIncome, 'expend' => $totalExpend, 'profit' => $totalProfit, 'inNum' => 0, 'outNum' => 0],
  602. ];
  603. $list = array_merge($arr, $statData);
  604. self::exportIncomeOutItem($list, $mainId);
  605. }
  606. util::success(['list' => $arr, 'packFreight' => $packFreight, 'onlyRefundCg' => $onlyRefundCg, 'onlyRefund' => $onlyRefund,
  607. 'totalIncome' => $totalIncome,
  608. 'totalExpend' => $totalExpend,
  609. 'totalProfit' => $totalProfit,
  610. 'tbSaleIncome' => $tbSaleIncome,
  611. 'tbCgIncome' => $tbCgIncome,
  612. 'tbCgIncomeStaff' => $tbCgIncomeStaff,
  613. 'tbSaleIncomeStaff' => $tbSaleIncomeStaff,
  614. 'totalLabourCost' => $totalLabourCost,
  615. 'totalDiscountAmount' => $totalDiscountAmount,
  616. ]);
  617. }
  618. //按分类统计收入 ssh 20231111
  619. public static function classProfile($mainId)
  620. {
  621. $get = Yii::$app->request->get();
  622. $export = $get['export'] ?? 0;
  623. $where = [];
  624. $where['mainId'] = $mainId;
  625. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  626. $startTime = $get['startTime'] ?? '';
  627. $endTime = $get['endTime'] ?? '';
  628. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  629. $start = $period['startTime'];
  630. $end = $period['endTime'];
  631. $where['time'] = ['between', [$start, $end]];
  632. $currentStartDate = date('Y-m-d', strtotime($start));
  633. $currentEndDate = date('Y-m-d', strtotime($end));
  634. $currentStartTime = $currentStartDate . ' 00:00:00';
  635. $currentEndTime = $currentEndDate . ' 23:59:59';
  636. $s = strtotime($currentStartTime);
  637. $e = strtotime($currentEndTime);
  638. $p = bcsub($e, $s);
  639. $xx = bcmul(86400, 90);
  640. if ($p > $xx) {
  641. util::fail('查询时间不能超过三个月');
  642. }
  643. $classList = ItemClassClass::getAllByCondition(['mainId' => $mainId], null, 'id,name', 'id');
  644. if (empty($classList)) {
  645. util::fail('没有找到花材');
  646. }
  647. $arr = [];
  648. //打包费和运费
  649. $packFreight = 0;
  650. $onlyRefundCg = 0;
  651. $onlyRefund = 0;
  652. $totalDiscountAmount = 0;
  653. $totalLabourCost = 0;
  654. //采购入库
  655. $tbCgIncome = [];
  656. $tbCgIncomeStaff = [];
  657. $cgWhere = ['mainId' => $mainId, 'status' => 4];
  658. $cgWhere['entryTime'] = ['between', [$currentStartTime, $currentEndTime]];
  659. $cgList = PurchaseOrderClass::getAllByCondition($cgWhere, null, '*');
  660. if (!empty($cgList)) {
  661. foreach ($cgList as $cgKey => $cgItem) {
  662. $packingCharge = $cgItem['packingCharge'] ?? 0;
  663. $shortCharge = $cgItem['shortCharge'] ?? 0;
  664. $longCharge = $cgItem['longCharge'] ?? 0;
  665. $pickCharge = $cgItem['pickCharge'] ?? 0;
  666. $localCharge = $cgItem['localCharge'] ?? 0;
  667. $cgStaffId = $cgItem['cgStaffId'] ?? 0;
  668. $cgStaffName = $cgItem['cgStaffName'] ?? '';
  669. $actPrice = $cgItem['actPrice'] ?? 0;
  670. if ($actPrice <= 0) {
  671. continue;
  672. }
  673. $packFreight = bcadd($packFreight, $packingCharge, 2);
  674. $packFreight = bcadd($packFreight, $shortCharge, 2);
  675. $packFreight = bcadd($packFreight, $longCharge, 2);
  676. $packFreight = bcadd($packFreight, $pickCharge, 2);
  677. $packFreight = bcadd($packFreight, $localCharge, 2);
  678. $tkPrice = $cgItem['tkPrice'] ?? 0;
  679. $orderSn = $cgItem['orderSn'] ?? '';
  680. $cgItemInfoList = PurchaseOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  681. if (!empty($cgItemInfoList)) {
  682. foreach ($cgItemInfoList as $cgData) {
  683. $itemNum = $cgData->itemNum ?? 0;
  684. $productId = $cgData->productId ?? 0;
  685. $name = $cgData->name ?? '';
  686. $refundNum = $cgData->refundNum ?? 0;
  687. $remainNum = bcsub($itemNum, $refundNum);
  688. $bigPrice = $cgData->bigPrice ?? 0;
  689. $amount = bcmul($remainNum, $bigPrice, 2);
  690. $classId = $cgData->classId;
  691. $className = $classList[$classId]['name'] ?? '已删除';
  692. if ($className == '运费') {
  693. //noticeUtil::push("{$cgData->orderSn}运费项", '15280215347');
  694. }
  695. $arr[$classId]['className'] = $className;
  696. $arr[$classId]['classId'] = $classId;
  697. if (isset($arr[$classId]['expend'])) {
  698. $arr[$classId]['expend'] = bcadd($amount, $arr[$classId]['expend'], 2);
  699. $arr[$classId]['cgNum'] = bcadd($arr[$classId]['cgNum'], $remainNum);
  700. } else {
  701. $arr[$classId]['expend'] = $amount;
  702. $arr[$classId]['income'] = 0;
  703. $arr[$classId]['cgNum'] = $remainNum;
  704. $arr[$classId]['saleNum'] = 0;
  705. }
  706. //单独统计
  707. $belongCost = $cgData->belongCost ?? 0;
  708. if ($belongCost == 1) {
  709. if (isset($tbCgIncome[$productId])) {
  710. $tbCgIncome[$productId]['num'] = bcadd($tbCgIncome[$productId]['num'], $remainNum);
  711. $tbCgIncome[$productId]['price'] = bcadd($tbCgIncome[$productId]['price'], $amount, 2);
  712. } else {
  713. $tbCgIncome[$productId] = ['num' => $remainNum, 'name' => $name, 'price' => $amount];
  714. }
  715. if (isset($tbCgIncomeStaff[$productId]) && isset($tbCgIncomeStaff[$productId][$cgStaffId])) {
  716. $tbCgIncomeStaff[$productId][$cgStaffId]['num'] = bcadd($tbCgIncomeStaff[$productId][$cgStaffId]['num'], $remainNum);
  717. $tbCgIncomeStaff[$productId][$cgStaffId]['price'] = bcadd($tbCgIncomeStaff[$productId][$cgStaffId]['price'], $amount, 2);
  718. } else {
  719. $tbCgIncomeStaff[$productId][$cgStaffId] = ['num' => $remainNum, 'name' => $name, 'price' => $amount, 'staffName' => $cgStaffName,];
  720. }
  721. }
  722. }
  723. if ($tkPrice > 0) {
  724. $cgRefundOrderList = CgRefundClass::getAllByCondition(['relateOrderSn' => $orderSn], null, '*', null, true);
  725. if (!empty($cgRefundOrderList)) {
  726. foreach ($cgRefundOrderList as $cgRefundOrder) {
  727. if ($cgRefundOrder->refundType == 2) {
  728. $onlyRefundCg = bcadd($cgRefundOrder->refundPrice, $onlyRefundCg, 2);
  729. }
  730. }
  731. }
  732. }
  733. }
  734. }
  735. }
  736. //批发开单
  737. $pfIncome = 0;
  738. $tbSaleIncome = [];
  739. $tbSaleIncomeStaff = [];
  740. $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  741. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  742. $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
  743. if (!empty($ghsOrderList)) {
  744. foreach ($ghsOrderList as $ghsKey => $ghsOrder) {
  745. $book = $ghsOrder['book'] ?? 0;
  746. $status = $ghsOrder['status'] ?? 0;
  747. if ($book == 1 && $status == 2) {
  748. //预订单待发货的不算收入
  749. continue;
  750. }
  751. if ($status == 0 || $status == 5) {
  752. continue;
  753. }
  754. $shopAdminId = $ghsOrder['shopAdminId'] ?? 0;
  755. $shopAdminName = $ghsOrder['shopAdminName'] ?? '';
  756. $orderSn = $ghsOrder['orderSn'] ?? '';
  757. $actPrice = $ghsOrder['actPrice'] ?? 0;
  758. if ($actPrice <= 0) {
  759. continue;
  760. }
  761. $tkPrice = $ghsOrder['tkPrice'] ?? 0;
  762. $pfIncome = bcadd($pfIncome, $actPrice, 2);
  763. $labourCost = $ghsOrder['labourCost'] ?? 0;
  764. $discountAmount = $ghsOrder['discountAmount'] ?? 0;
  765. $totalLabourCost = bcadd($labourCost, $totalLabourCost, 2);
  766. $totalDiscountAmount = bcadd($totalDiscountAmount, $discountAmount, 2);
  767. $orderItemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  768. if (!empty($orderItemList)) {
  769. foreach ($orderItemList as $orderItem) {
  770. $refundNum = $orderItem->refundNum ?? 0;
  771. $name = $orderItem->name ?? '';
  772. $productId = $orderItem->productId ?? 0;
  773. $num = $orderItem->xhNum ?? 0;
  774. $remainNum = bcsub($num, $refundNum);
  775. $classId = $orderItem->classId ?? 0;
  776. $className = $classList[$classId]['name'] ?? '已删除';
  777. $xhUnitPrice = $orderItem->xhUnitPrice ?? 0;
  778. $amount = bcmul($remainNum, $xhUnitPrice, 2);
  779. $arr[$classId]['className'] = $className;
  780. $arr[$classId]['classId'] = $classId;
  781. if (isset($arr[$classId]['income'])) {
  782. $arr[$classId]['income'] = bcadd($amount, $arr[$classId]['income'], 2);
  783. $arr[$classId]['saleNum'] = bcadd($arr[$classId]['saleNum'], $remainNum);
  784. } else {
  785. $arr[$classId]['expend'] = 0;
  786. $arr[$classId]['income'] = $amount;
  787. $arr[$classId]['saleNum'] = $remainNum;
  788. $arr[$classId]['cgNum'] = 0;
  789. }
  790. //单独统计
  791. $belongCost = $orderItem->belongCost ?? 0;
  792. if ($belongCost == 1) {
  793. if (isset($tbSaleIncome[$productId])) {
  794. $tbSaleIncome[$productId]['num'] = bcadd($tbSaleIncome[$productId]['num'], $remainNum);
  795. $tbSaleIncome[$productId]['price'] = bcadd($tbSaleIncome[$productId]['price'], $amount, 2);
  796. } else {
  797. $tbSaleIncome[$productId] = ['name' => $name, 'num' => $remainNum, 'price' => $amount];
  798. }
  799. if (isset($tbSaleIncomeStaff[$productId]) && isset($tbSaleIncomeStaff[$productId][$shopAdminId])) {
  800. $tbSaleIncomeStaff[$productId][$shopAdminId]['num'] = bcadd($tbSaleIncomeStaff[$productId][$shopAdminId]['num'], $remainNum);
  801. $tbSaleIncomeStaff[$productId][$shopAdminId]['price'] = bcadd($tbSaleIncomeStaff[$productId][$shopAdminId]['price'], $amount, 2);
  802. } else {
  803. $tbSaleIncomeStaff[$productId][$shopAdminId] = ['name' => $name, 'num' => $remainNum, 'price' => $amount, 'staffName' => $shopAdminName];
  804. }
  805. }
  806. }
  807. if ($tkPrice > 0) {
  808. $refundList = RefundOrderClass::getAllByCondition(['relateOrderSn' => $orderSn], null, '*', null, true);
  809. if (!empty($refundList)) {
  810. foreach ($refundList as $refund) {
  811. if ($refund->refundType == 1) {
  812. //退货退款前面已经考虑,这里不再处理 OK
  813. } else {
  814. $onlyRefund = bcadd($onlyRefund, $refund->refundPrice, 2);
  815. }
  816. }
  817. }
  818. }
  819. }
  820. }
  821. }
  822. //零售开单
  823. $lsIncome = 0;
  824. $hdWhere = ['mainId' => $mainId, 'status' => ['in', [HdOrderClass::ORDER_STATUS_UN_SEND, HdOrderClass::ORDER_STATUS_SENDING, HdOrderClass::ORDER_STATUS_COMPLETE]]];
  825. $hdWhere['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  826. $hdOrderList = HdOrderClass::getAllByCondition($hdWhere, null, '*');
  827. foreach ($hdOrderList as $hdKey => $hdOrder) {
  828. $actPrice = $hdOrder['mainPay'] ?? 0;
  829. $cash = $hdOrder['cash'] ?? 0;
  830. if ($actPrice <= 0 && $cash <= 0) {
  831. continue;
  832. }
  833. $lsIncome = bcadd($actPrice, $lsIncome, 2);
  834. $lsIncome = bcadd($lsIncome, $cash, 2);
  835. $orderSn = $hdOrder['orderSn'] ?? '';
  836. $tkPrice = $hdOrder['tkPrice'] ?? 0;
  837. $orderItemList = \bizHd\order\classes\OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  838. if (!empty($orderItemList)) {
  839. foreach ($orderItemList as $orderItem) {
  840. $classId = $orderItem->classId ?? 0;
  841. $unitPrice = $orderItem->unitPrice ?? 0;
  842. $refundNum = $orderItem->refundNum ?? 0;
  843. $num = $orderItem->num ?? 0;
  844. $productId = $orderItem->itemId ?? 0;
  845. $name = $orderItem->name ?? '';
  846. $remainNum = bcsub($num, $refundNum);
  847. $amount = bcmul($remainNum, $unitPrice, 2);
  848. $className = $classList[$classId]['name'] ?? '已删除';
  849. $arr[$classId]['className'] = $className;
  850. $arr[$classId]['classId'] = $classId;
  851. if (isset($arr[$classId]['income'])) {
  852. $arr[$classId]['income'] = bcadd($amount, $arr[$classId]['income'], 2);
  853. $arr[$classId]['saleNum'] = bcadd($arr[$classId]['saleNum'], $remainNum);
  854. } else {
  855. $arr[$classId]['expend'] = 0;
  856. $arr[$classId]['income'] = $amount;
  857. $arr[$classId]['saleNum'] = $remainNum;
  858. $arr[$classId]['cgNum'] = 0;
  859. }
  860. $belongCost = $orderItem->belongCost ?? 0;
  861. if ($belongCost == 1) {
  862. if (isset($tbSaleIncome[$productId])) {
  863. $tbSaleIncome[$productId]['num'] = bcadd($tbSaleIncome[$productId]['num'], $remainNum);
  864. $tbSaleIncome[$productId]['price'] = bcadd($tbSaleIncome[$productId]['price'], $amount, 2);
  865. } else {
  866. $tbSaleIncome[$productId] = ['name' => $name, 'num' => $remainNum, 'price' => $amount];
  867. }
  868. }
  869. }
  870. if ($tkPrice > 0) {
  871. $refundList = HdRefundClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  872. if (!empty($refundList)) {
  873. foreach ($refundList as $refund) {
  874. if ($refund->refundType == 2) {
  875. $onlyRefund = bcadd($onlyRefund, $refund->refundPrice, 2);
  876. }
  877. }
  878. }
  879. }
  880. }
  881. }
  882. $totalIncome = 0;
  883. $totalExpend = 0;
  884. if (!empty($arr)) {
  885. foreach ($arr as $key => $val) {
  886. $income = $val['income'] ?? 0;
  887. $income = floatval($income);
  888. $totalIncome = bcadd($totalIncome, $income, 2);
  889. $expend = $val['expend'] ?? 0;
  890. $expend = floatval($expend);
  891. $totalExpend = bcadd($totalExpend, $expend, 2);
  892. $profit = bcsub($income, $expend, 2);
  893. $arr[$key]['profit'] = floatval($profit);
  894. $arr[$key]['income'] = floatval($income);
  895. $arr[$key]['expend'] = floatval($expend);
  896. }
  897. }
  898. //总成本算上物流打包等费用
  899. $totalExpend = bcadd($totalExpend, $packFreight, 2);
  900. //总收入算上采购仅退款
  901. $totalIncome = bcadd($totalIncome, $onlyRefundCg, 2);
  902. //总收入加上人工费
  903. $totalIncome = bcadd($totalIncome, $totalLabourCost, 2);
  904. //总收入减掉优惠金额
  905. $totalIncome = bcsub($totalIncome, $totalDiscountAmount, 2);
  906. //总成本算上销售仅退款
  907. $totalExpend = bcadd($totalExpend, $onlyRefund, 2);
  908. $totalProfit = bcsub($totalIncome, $totalExpend, 2);
  909. $totalIncome = floatval($totalIncome);
  910. $totalExpend = floatval($totalExpend);
  911. $totalProfit = floatval($totalProfit);
  912. $arr = array_values($arr);
  913. $arr = arrayUtil::arraySort($arr, 'profit');
  914. if ($export == 1) {
  915. $statData = [
  916. ['classId' => 0, 'className' => '物流打包费', 'income' => 0, 'expend' => $packFreight, 'profit' => -$packFreight, 'saleNum' => 0, 'cgNum' => 0],
  917. ['classId' => 0, 'className' => '采购仅退款', 'income' => $onlyRefundCg, 'expend' => 0, 'profit' => $onlyRefundCg, 'saleNum' => 0, 'cgNum' => 0],
  918. ['classId' => 0, 'className' => '销售仅退款', 'income' => 0, 'expend' => $onlyRefund, 'profit' => -$onlyRefund, 'saleNum' => 0, 'cgNum' => 0],
  919. ['classId' => 0, 'className' => '附加人工费', 'income' => $totalLabourCost, 'expend' => 0, 'profit' => $totalLabourCost, 'saleNum' => 0, 'cgNum' => 0],
  920. ['classId' => 0, 'className' => '优惠金额', 'income' => -$totalDiscountAmount, 'expend' => 0, 'profit' => -$totalDiscountAmount, 'saleNum' => 0, 'cgNum' => 0],
  921. ['classId' => 0, 'className' => '【汇总金额】', 'income' => $totalIncome, 'expend' => $totalExpend, 'profit' => $totalProfit, 'saleNum' => 0, 'cgNum' => 0],
  922. ];
  923. $list = array_merge($arr, $statData);
  924. self::exportIncomeClass($list, $mainId);
  925. }
  926. util::success(['list' => $arr, 'packFreight' => $packFreight, 'onlyRefundCg' => $onlyRefundCg, 'onlyRefund' => $onlyRefund,
  927. 'totalIncome' => $totalIncome,
  928. 'totalExpend' => $totalExpend,
  929. 'totalProfit' => $totalProfit,
  930. 'tbSaleIncome' => $tbSaleIncome,
  931. 'tbCgIncome' => $tbCgIncome,
  932. 'tbCgIncomeStaff' => $tbCgIncomeStaff,
  933. 'tbSaleIncomeStaff' => $tbSaleIncomeStaff,
  934. 'totalLabourCost' => $totalLabourCost,
  935. 'totalDiscountAmount' => $totalDiscountAmount,
  936. ]);
  937. }
  938. public static function exportIncomeOutItem($list, $mainId)
  939. {
  940. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  941. require_once($phpExcelFile . 'Classes/PHPExcel.php');
  942. $objPHPExcel = new \PHPExcel();
  943. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
  944. ->setLastModifiedBy("Maarten Balliauw")
  945. ->setTitle("Office 2007 XLSX Document")
  946. ->setSubject("Office 2007 XLSX Document")
  947. ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
  948. ->setKeywords("office 2007 openxml php")
  949. ->setCategory("file");
  950. $ghsTitle = '供货商';
  951. $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " " . $ghsTitle);
  952. $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(0.7);
  953. $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(0.1);
  954. $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.5);
  955. $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0);
  956. $objPHPExcel->getActiveSheet()->getPageMargins()->setHeader(0.1);
  957. $objPHPExcel->getActiveSheet()->getPageMargins()->setFooter(0);
  958. $objPHPExcel->getActiveSheet()->setCellValue('A1', '名称');
  959. $objPHPExcel->getActiveSheet()->setCellValue('B1', '销售收入');
  960. $objPHPExcel->getActiveSheet()->setCellValue('C1', '销售数量');
  961. $objPHPExcel->getActiveSheet()->setCellValue('D1', '采购成本');
  962. $objPHPExcel->getActiveSheet()->setCellValue('E1', '采购数量');
  963. $objPHPExcel->getActiveSheet()->setCellValue('F1', '利润');
  964. //设置宽度
  965. $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(30);
  966. $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
  967. $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
  968. $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
  969. //加粗
  970. $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(9)->setBold(true);
  971. $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(9)->setBold(true);
  972. $objPHPExcel->getActiveSheet()->getStyle('C1')->getFont()->setSize(9)->setBold(true);
  973. $objPHPExcel->getActiveSheet()->getStyle('D1')->getFont()->setSize(9)->setBold(true);
  974. $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  975. $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  976. $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  977. $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  978. $baseRow = 2;
  979. foreach ($list as $key => $custom) {
  980. $i = $baseRow + $key;
  981. $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $custom['name']);
  982. $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $custom['income']);
  983. $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $custom['inNum']);
  984. $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $custom['expend']);
  985. $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $custom['outNum']);
  986. $objPHPExcel->getActiveSheet()->setCellValue('F' . $i, $custom['profit']);
  987. //居左
  988. $objPHPExcel->getActiveSheet()->getStyle('A' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  989. $objPHPExcel->getActiveSheet()->getStyle('B' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  990. $objPHPExcel->getActiveSheet()->getStyle('C' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  991. $objPHPExcel->getActiveSheet()->getStyle('D' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  992. $objPHPExcel->getActiveSheet()->getStyle('E' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  993. $objPHPExcel->getActiveSheet()->getStyle('F' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  994. }
  995. $fileName = '收支花材-' . date("m-d");
  996. $objPHPExcel->getActiveSheet()->setTitle($fileName);
  997. $objPHPExcel->setActiveSheetIndex(0);
  998. $dir = './priceTable/' . $mainId;
  999. if (file_exists($dir) == false) {
  1000. mkdir($dir, 0777, true);
  1001. }
  1002. $date = $fileName;
  1003. $file = $date . '.xls';
  1004. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  1005. if (file_exists($dir . '/' . $file)) {
  1006. unlink($dir . '/' . $file);
  1007. }
  1008. $objWriter->save($dir . '/' . $file);
  1009. $fileUrl = Yii::$app->params['ghsHost'] . '/priceTable/' . $mainId . '/' . $file;
  1010. util::success(['file' => $fileUrl, 'shortFile' => $file]);
  1011. }
  1012. public static function exportIncomeClass($list, $mainId)
  1013. {
  1014. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  1015. require_once($phpExcelFile . 'Classes/PHPExcel.php');
  1016. $objPHPExcel = new \PHPExcel();
  1017. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
  1018. ->setLastModifiedBy("Maarten Balliauw")
  1019. ->setTitle("Office 2007 XLSX Document")
  1020. ->setSubject("Office 2007 XLSX Document")
  1021. ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
  1022. ->setKeywords("office 2007 openxml php")
  1023. ->setCategory("file");
  1024. $ghsTitle = '供货商';
  1025. $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " " . $ghsTitle);
  1026. $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(0.7);
  1027. $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(0.1);
  1028. $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.5);
  1029. $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0);
  1030. $objPHPExcel->getActiveSheet()->getPageMargins()->setHeader(0.1);
  1031. $objPHPExcel->getActiveSheet()->getPageMargins()->setFooter(0);
  1032. $objPHPExcel->getActiveSheet()->setCellValue('A1', '名称');
  1033. $objPHPExcel->getActiveSheet()->setCellValue('B1', '销售数量');
  1034. $objPHPExcel->getActiveSheet()->setCellValue('C1', '销售收入');
  1035. $objPHPExcel->getActiveSheet()->setCellValue('D1', '采购数量');
  1036. $objPHPExcel->getActiveSheet()->setCellValue('E1', '采购成本');
  1037. $objPHPExcel->getActiveSheet()->setCellValue('F1', '利润');
  1038. //设置宽度
  1039. $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
  1040. //加粗
  1041. $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(9)->setBold(true);
  1042. $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(9)->setBold(true);
  1043. $objPHPExcel->getActiveSheet()->getStyle('C1')->getFont()->setSize(9)->setBold(true);
  1044. $objPHPExcel->getActiveSheet()->getStyle('D1')->getFont()->setSize(9)->setBold(true);
  1045. $objPHPExcel->getActiveSheet()->getStyle('E1')->getFont()->setSize(9)->setBold(true);
  1046. $objPHPExcel->getActiveSheet()->getStyle('F1')->getFont()->setSize(9)->setBold(true);
  1047. $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1048. $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1049. $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1050. $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1051. $objPHPExcel->getActiveSheet()->getStyle('E1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1052. $objPHPExcel->getActiveSheet()->getStyle('F1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1053. $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1054. $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1055. $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1056. $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1057. $objPHPExcel->getActiveSheet()->getStyle('E1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1058. $objPHPExcel->getActiveSheet()->getStyle('F1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1059. $baseRow = 2;
  1060. foreach ($list as $key => $custom) {
  1061. $i = $baseRow + $key;
  1062. $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $custom['className']);
  1063. $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $custom['saleNum']);
  1064. $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $custom['income']);
  1065. $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $custom['cgNum']);
  1066. $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $custom['expend']);
  1067. $objPHPExcel->getActiveSheet()->setCellValue('F' . $i, $custom['profit']);
  1068. //居左
  1069. $objPHPExcel->getActiveSheet()->getStyle('A' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1070. $objPHPExcel->getActiveSheet()->getStyle('B' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1071. $objPHPExcel->getActiveSheet()->getStyle('C' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1072. $objPHPExcel->getActiveSheet()->getStyle('D' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1073. $objPHPExcel->getActiveSheet()->getStyle('E' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1074. $objPHPExcel->getActiveSheet()->getStyle('F' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1075. }
  1076. $fileName = '收支分类-' . time();
  1077. $objPHPExcel->getActiveSheet()->setTitle($fileName);
  1078. $objPHPExcel->setActiveSheetIndex(0);
  1079. $dir = './priceTable/' . $mainId;
  1080. if (file_exists($dir) == false) {
  1081. mkdir($dir, 0777, true);
  1082. }
  1083. $date = $fileName;
  1084. $file = $date . '.xls';
  1085. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  1086. if (file_exists($dir . '/' . $file)) {
  1087. unlink($dir . '/' . $file);
  1088. }
  1089. $objWriter->save($dir . '/' . $file);
  1090. $fileUrl = Yii::$app->params['ghsHost'] . '/priceTable/' . $mainId . '/' . $file;
  1091. util::success(['file' => $fileUrl, 'shortFile' => $file]);
  1092. }
  1093. public static function exportItemSale($list, $mainId)
  1094. {
  1095. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  1096. require_once($phpExcelFile . 'Classes/PHPExcel.php');
  1097. $objPHPExcel = new \PHPExcel();
  1098. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
  1099. ->setLastModifiedBy("Maarten Balliauw")
  1100. ->setTitle("Office 2007 XLSX Document")
  1101. ->setSubject("Office 2007 XLSX Document")
  1102. ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
  1103. ->setKeywords("office 2007 openxml php")
  1104. ->setCategory("file");
  1105. $ghsTitle = '供货商';
  1106. $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " " . $ghsTitle);
  1107. $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(0.7);
  1108. $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(0.1);
  1109. $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.5);
  1110. $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0);
  1111. $objPHPExcel->getActiveSheet()->getPageMargins()->setHeader(0.1);
  1112. $objPHPExcel->getActiveSheet()->getPageMargins()->setFooter(0);
  1113. $objPHPExcel->getActiveSheet()->setCellValue('A1', '名称');
  1114. $objPHPExcel->getActiveSheet()->setCellValue('B1', '总数量');
  1115. $objPHPExcel->getActiveSheet()->setCellValue('C1', '总金额');
  1116. $objPHPExcel->getActiveSheet()->setCellValue('D1', '毛利');
  1117. $objPHPExcel->getActiveSheet()->setCellValue('E1', '毛利率');
  1118. //设置宽度
  1119. $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(30);
  1120. $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
  1121. $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
  1122. $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
  1123. $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20);
  1124. //加粗
  1125. $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(9)->setBold(true);
  1126. $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(9)->setBold(true);
  1127. $objPHPExcel->getActiveSheet()->getStyle('C1')->getFont()->setSize(9)->setBold(true);
  1128. $objPHPExcel->getActiveSheet()->getStyle('D1')->getFont()->setSize(9)->setBold(true);
  1129. $objPHPExcel->getActiveSheet()->getStyle('E1')->getFont()->setSize(9)->setBold(true);
  1130. $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1131. $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1132. $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1133. $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1134. $objPHPExcel->getActiveSheet()->getStyle('E1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1135. $baseRow = 2;
  1136. foreach ($list as $key => $custom) {
  1137. $i = $baseRow + $key;
  1138. $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $custom['name']);
  1139. $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $custom['num']);
  1140. $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $custom['amount']);
  1141. $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $custom['gross']);
  1142. $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $custom['mll'] . '%');
  1143. //居左
  1144. $objPHPExcel->getActiveSheet()->getStyle('A' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1145. $objPHPExcel->getActiveSheet()->getStyle('B' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1146. $objPHPExcel->getActiveSheet()->getStyle('C' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1147. $objPHPExcel->getActiveSheet()->getStyle('D' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1148. $objPHPExcel->getActiveSheet()->getStyle('E' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1149. }
  1150. $fileName = '花材销量-' . date("m-d");
  1151. $objPHPExcel->getActiveSheet()->setTitle($fileName);
  1152. $objPHPExcel->setActiveSheetIndex(0);
  1153. $dir = './priceTable/' . $mainId;
  1154. if (file_exists($dir) == false) {
  1155. mkdir($dir, 0777, true);
  1156. }
  1157. $date = $fileName;
  1158. $file = $date . '.xls';
  1159. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  1160. if (file_exists($dir . '/' . $file)) {
  1161. unlink($dir . '/' . $file);
  1162. }
  1163. $objWriter->save($dir . '/' . $file);
  1164. $fileUrl = Yii::$app->params['ghsHost'] . '/priceTable/' . $mainId . '/' . $file;
  1165. util::success(['file' => $fileUrl, 'shortFile' => $file]);
  1166. }
  1167. public static function send($mainId)
  1168. {
  1169. $get = Yii::$app->request->get();
  1170. $where = [];
  1171. $where['mainId'] = $mainId;
  1172. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  1173. $startTime = $get['startTime'] ?? '';
  1174. $endTime = $get['endTime'] ?? '';
  1175. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  1176. $start = $period['startTime'];
  1177. $end = $period['endTime'];
  1178. $where['time'] = ['between', [$start, $end]];
  1179. $currentStartDate = date('Y-m-d', strtotime($start));
  1180. $currentEndDate = date('Y-m-d', strtotime($end));
  1181. $currentStartTime = $currentStartDate . ' 00:00:00';
  1182. $currentEndTime = $currentEndDate . ' 23:59:59';
  1183. $s = strtotime($currentStartTime);
  1184. $e = strtotime($currentEndTime);
  1185. $p = bcsub($e, $s);
  1186. $xx = bcmul(86400, 90);
  1187. if ($p > $xx) {
  1188. util::fail('查询时间不能超过三个月');
  1189. }
  1190. $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  1191. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  1192. $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
  1193. $arr = [];
  1194. if (!empty($ghsOrderList)) {
  1195. foreach ($ghsOrderList as $ghsKey => $ghsOrder) {
  1196. $book = $ghsOrder['book'] ?? 0;
  1197. $status = $ghsOrder['status'] ?? 0;
  1198. if ($book == 1 && $status == 2) {
  1199. //预订单待发货的不算收入
  1200. continue;
  1201. }
  1202. if ($status == 0 || $status == 5) {
  1203. continue;
  1204. }
  1205. $orderSn = $ghsOrder['orderSn'] ?? '';
  1206. $actPrice = $ghsOrder['actPrice'] ?? 0;
  1207. if ($actPrice <= 0) {
  1208. continue;
  1209. }
  1210. $sendStaffId = $ghsOrder['sendStaffId'] ?? 0;
  1211. $sendStaffName = isset($ghsOrder['sendStaffName']) && !empty($ghsOrder['sendStaffName']) ? $ghsOrder['sendStaffName'] : '未记名';
  1212. if (isset($arr[$sendStaffId])) {
  1213. $arr[$sendStaffId]['orderNum'] = bcadd($arr[$sendStaffId]['orderNum'], 1);
  1214. $arr[$sendStaffId]['amount'] = bcadd($arr[$sendStaffId]['amount'], $actPrice, 2);
  1215. } else {
  1216. $arr[$sendStaffId] = ['staffName' => $sendStaffName, 'orderNum' => 1, 'itemNum' => 0, 'amount' => $actPrice, 'staffId' => $sendStaffId];
  1217. }
  1218. $orderItemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  1219. if (!empty($orderItemList)) {
  1220. foreach ($orderItemList as $orderItem) {
  1221. $refundNum = $orderItem->refundNum ?? 0;
  1222. $num = $orderItem->xhNum ?? 0;
  1223. $remainNum = bcsub($num, $refundNum);
  1224. $arr[$sendStaffId]['itemNum'] = bcadd($arr[$sendStaffId]['itemNum'], $remainNum);
  1225. }
  1226. }
  1227. }
  1228. }
  1229. if (!empty($arr)) {
  1230. $arr = array_values($arr);
  1231. }
  1232. return $arr;
  1233. }
  1234. }